OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / opencog-kb / language-learning / mvp / profile-gpu.scm

139 lines · scm ref:
#!/usr/bin/env guile
!#
;;
;; profile-gpu.scm - Deep profile GPU PMFG to find the 25s bottleneck
;;

(use-modules (opencog) (opencog exec))
(use-modules (opencog persist) (opencog persist-rocks))
(use-modules (opencog sensory) (opencog opencl))
(use-modules (opencog lg))
(use-modules (srfi srfi-1))

(display "\n═══ GPU PMFG Deep Profiling ═══\n\n")

;; Quick MI cache (just for profiling - use hash table)
(define *mi-cache* (make-hash-table))
(hash-set! *mi-cache* (cons "she" "looked") 2.29)
(hash-set! *mi-cache* (cons "she" "at") 0.34)
(hash-set! *mi-cache* (cons "she" "him") 0.36)
(hash-set! *mi-cache* (cons "looked" "at") 3.55)
(hash-set! *mi-cache* (cons "looked" "him") 0.0)
(hash-set! *mi-cache* (cons "at" "him") -0.20)

(define (get-mi w1 w2)
  (or (hash-ref *mi-cache* (cons w1 w2)) 0.0))

;; Profile helper
(define (profile name thunk)
  (let* ((t0 (get-internal-real-time))
         (result (thunk))
         (elapsed (/ (- (get-internal-real-time) t0) 1000.0)))
    (format #t "  ~a: ~,2f ms\n" name elapsed)
    result))

;; Test words
(define words '("she" "looked" "at" "him"))
(define n (length words))

(display "Test: 4 words\n\n")

;; Step 1: Build MI matrix
(display "=== Step-by-step profiling ===\n")

(define mi-list
  (profile "1. Build MI list (n² lookups)"
    (lambda ()
      (let ((lst (make-list (* n n) 0.0)))
        (do ((i 0 (+ i 1))) ((>= i n))
          (do ((j 0 (+ j 1))) ((>= j n))
            (list-set! lst (+ (* i n) j)
                       (get-mi (list-ref words i) (list-ref words j)))))
        lst))))

;; Step 2: Create FloatValue
(define fv-mi
  (profile "2. Create FloatValue (MI)"
    (lambda () (apply FloatValue mi-list))))

;; Step 3: Create other values
(profile "3. Create FloatValue (words, nw, vs)"
  (lambda ()
    (apply FloatValue (iota n))
    (FloatValue (exact->inexact n))
    (FloatValue (exact->inexact n))))

;; Step 4: Create OpenclFloatValue
(define ofv-edges #f)
(define ofv-ne #f)
(profile "4. Create OpenclFloatValue (output buffers)"
  (lambda ()
    (set! ofv-edges (apply OpenclFloatValue (make-list (* 2 (* 3 n)) -1.0)))
    (set! ofv-ne (OpenclFloatValue -1.0))))

;; Step 5: Set values on anchors
(profile "5. cog-set-value! (6 calls)"
  (lambda ()
    (cog-set-value! (Anchor "prof") (Predicate "mi") fv-mi)
    (cog-set-value! (Anchor "prof") (Predicate "words") (apply FloatValue (iota n)))
    (cog-set-value! (Anchor "prof") (Predicate "nw") (FloatValue (exact->inexact n)))
    (cog-set-value! (Anchor "prof") (Predicate "vs") (FloatValue (exact->inexact n)))
    (cog-set-value! (Anchor "prof") (Predicate "edges") ofv-edges)
    (cog-set-value! (Anchor "prof") (Predicate "ne") ofv-ne)))

;; Step 6: Open GPU kernel
(display "\n=== GPU Kernel Operations ===\n")

(define gpu #f)
(profile "6. Create OpenclNode + open kernel"
  (lambda ()
    (set! gpu (OpenclNode "opencl://:/tmp/pmfg-kernel.cl"))
    (cog-execute! (SetValue gpu (Predicate "*-open-*") (Type 'FloatValue)))))

;; Step 7: GPU write
(profile "7. GPU write (SetValue *-write-*)"
  (lambda ()
    (cog-execute!
      (SetValue gpu (Predicate "*-write-*")
        (Section (Item "pmfg_single")
          (ConnectorSeq
            (ValueOf (Anchor "prof") (Predicate "mi"))
            (ValueOf (Anchor "prof") (Predicate "words"))
            (ValueOf (Anchor "prof") (Predicate "nw"))
            (ValueOf (Anchor "prof") (Predicate "vs"))
            (ValueOf (Anchor "prof") (Predicate "edges"))
            (ValueOf (Anchor "prof") (Predicate "ne"))))))))

;; Step 8: GPU read
(profile "8. GPU read (ValueOf *-read-*)"
  (lambda ()
    (cog-execute! (ValueOf gpu (Predicate "*-read-*")))))

;; Step 9: Extract results
(profile "9. Extract results"
  (lambda ()
    (cog-execute! (ValueOf (Anchor "prof") (Predicate "ne")))
    (cog-execute! (ValueOf (Anchor "prof") (Predicate "edges")))))

(display "\n=== Second run (kernel already compiled) ===\n")

;; Run again to see if kernel compilation was the issue
(profile "7b. GPU write (second time)"
  (lambda ()
    (cog-execute!
      (SetValue gpu (Predicate "*-write-*")
        (Section (Item "pmfg_single")
          (ConnectorSeq
            (ValueOf (Anchor "prof") (Predicate "mi"))
            (ValueOf (Anchor "prof") (Predicate "words"))
            (ValueOf (Anchor "prof") (Predicate "nw"))
            (ValueOf (Anchor "prof") (Predicate "vs"))
            (ValueOf (Anchor "prof") (Predicate "edges"))
            (ValueOf (Anchor "prof") (Predicate "ne"))))))))

(profile "8b. GPU read (second time)"
  (lambda ()
    (cog-execute! (ValueOf gpu (Predicate "*-read-*")))))

(display "\nDone.\n")