Skip to content

Commit

Permalink
Enhancement: Global Runtime (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
hikettei authored Jan 11, 2025
1 parent e7f7bba commit 100f31f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 13 deletions.
6 changes: 2 additions & 4 deletions external/gguf/tensor-info.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,11 @@
(ggml-type->caten-type (tensor-info-ggml-type tensor-info))
nil
:device 'caten/byoc/lisp:LispBuffer)))
(setf (caten/runtime:buffer-value buffer) (tensor-info-buffer tensor-info))
(caten/runtime:open-buffer (caten/api:get-global-runtime) buffer)
(caten/runtime:transfer-from-array (caten/api:get-global-runtime) buffer (tensor-info-buffer tensor-info))
(let ((out (caten/api:make-tensor
(reverse (tensor-info-dimensions tensor-info))
:dtype (ggml-type->caten-type (tensor-info-ggml-type tensor-info))
:from buffer)))
(setf (caten/api:tensor-buffer out) buffer)
;; [TODO] caten/runtime:open-buffer depends on the runtime,
;; Transfer the array from cpu into gpu?
;; (!send out (caten/codegen/backend:get-buffer-type))
out)))
1 change: 1 addition & 0 deletions source/api/documentation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Compute the backward pass of the compiled computational graph (GraphRuntime). No
(docs:doc/macro "with-no-grad" 'with-no-grad)
(docs:doc/variable "*inference-mode*" '*inference-mode*)
(docs:doc/macro "with-inference-mode" 'with-inference-mode)
(docs:doc/function "get-global-runtime" #'get-global-runtime)

(docs:section "Examples")
(docs:subsection "Tensor Creation")
Expand Down
16 changes: 11 additions & 5 deletions source/api/facets.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ Users can extend this method if needed.
(caten/common.dtype:lisp->dtype (array-element-type obj))))
(buffer (make-buffer (array-dimensions obj) (static-compute-strides *default-order* (array-dimensions obj)) dtype nil :device (caten/codegen/backend:get-buffer-type)))
;; TODO: Transfer into device without initializing runtime
(_ (setf (buffer-value buffer) storage))
(_ (open-buffer (get-global-runtime) buffer))
(__ (transfer-from-array (get-global-runtime) buffer storage))
(place (make-tensor (array-dimensions obj) :dtype dtype :from buffer)))
(declare (ignore _))
(declare (ignore _ __))
(setf (tensor-buffer place) buffer)
place))

Expand All @@ -71,16 +72,21 @@ Users can extend this method if needed.
(assert (tensor-buffer obj) () "The tensor ~a is not realized." obj)
(transfer-into-array (tensor-buffer obj)))

(defmacro with-facet ((bind (object &key (direction :array))) &body body)
(defun synchronize-facet (placeholder bind direction)
(when (and (tensor-p placeholder) (not (eql direction :tensor)))
(transfer-from-array (get-global-runtime) (tensor-buffer placeholder) (change-facet (change-facet bind :tensor) :simple-array))))

(defmacro with-facet ((bind (object &key (direction :array))) &body body &aux (placeholder (gensym)))
"
```
(with-facet (bind (object &key (direction :array))) &body body)
```
Binds the result of `(change-facet object direction)` to the `bind`.
"
`(let ((,bind (change-facet ,object ,direction)))
,@body))
`(let ((,placeholder ,object))
(let ((,bind (change-facet ,placeholder ,direction)))
(prog1 ,@body (synchronize-facet ,placeholder ,bind ,direction)))))

(defmacro with-facets ((&rest input-forms) &body body)
"
Expand Down
2 changes: 2 additions & 0 deletions source/api/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

;; from tensor.lisp
(:export
#:*global-runtime*
#:get-global-runtime
#:make-tensor
#:make-view-internal
#:tensor-graph
Expand Down
12 changes: 12 additions & 0 deletions source/api/tensor.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,15 @@ Returns `:INF` if the number is negative infinity, `:-INF` if the number is nega
(if (> x 0) :inf :-inf))
((float-nan-p x) :nan)
(t t)))
;; ~~~ Temporary Runtime Management ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(defparameter *global-runtime* (make-hash-table))

(defun get-global-runtime ()
"
```
(get-global-runtime)
```
Returns a temporary runtime object just used for allocation global buffer."
(or (gethash (ctx:getenv :BACKEND) *global-runtime*)
(setf (gethash (ctx:getenv :BACKEND) *global-runtime*)
(make-runtime (make-graph) :runtime (caten/codegen/backend:get-runtime-type) :buffer-type (caten/codegen/backend:get-buffer-type)))))
6 changes: 4 additions & 2 deletions source/byoc/metal.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@

(defmethod initialize-instance :after ((runtime MetalRuntime) &key)
(ensure-foreign-library)
(assert (runtime-renderer runtime))
(with-float-traps-masked t
(setf (metal-runtime-device runtime) (metal-renderer-device (runtime-renderer runtime)))))
(setf (metal-runtime-device runtime)
(if (runtime-renderer runtime)
(metal-renderer-device (runtime-renderer runtime))
(MTLCreateSystemDefaultDevice)))))

(defmethod open-buffer ((runtime MetalRuntime) (buffer MetalBuffer))
(let ((initial-value (if (eql (buffer-dtype buffer) :bool)
Expand Down
2 changes: 1 addition & 1 deletion source/codegen/auto-scheduler/ast-parser.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ scop.lisp for the opposite things.
(let* ((directive (str->directive (cffi:foreign-string-to-lisp (isl::%isl-id-get-name (isl::%isl-ast-node-mark-get-id ast)))))
(user (parse-isl-ast ctx (isl::%isl-ast-node-mark-get-node ast))))
;; A Sequence of Mark => Set Priority
;; ISL Schedule Levelで事前にSortしておく?(e.g.: GLOBALはUNROLLの後に来ないといけない)
;; [TODO] Sort marks in the isl-schedule level (e.g.: GLOBAL should come after UNROLL)
(macrolet ((is (name) `(equalp (directive-type directive) ,name)))
(typecase user
(AstFor
Expand Down
7 changes: 6 additions & 1 deletion source/codegen/backend.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#:get-backend-auto-scheduler
#:get-backend-jit-p
#:jit-mode-p
#:get-buffer-type))
#:get-buffer-type
#:get-runtime-type))

(in-package :caten/codegen/backend)

Expand Down Expand Up @@ -39,3 +40,7 @@ Registers a new backend.
(defun get-buffer-type (&key (backend (ctx:getenv :BACKEND)))
"Returns the buffer type for the current device."
(get-backend-buffer backend))

(defun get-runtime-type (&key (backend (ctx:getenv :BACKEND)))
"Returns the runtime type for the current device."
(get-backend-runtime backend))

0 comments on commit 100f31f

Please sign in to comment.