Caten Documentation

  • Home
  • Quickstart
  • Development
  • API Reference
    • caten/air
    • caten/aasm
    • caten/codegen
    • caten/api
      • Overview
      • Tensor
      • Func
      • Module
      • Model
      • Initializers
      • ShapeTracker
      • Facet API
      • StateDict
    • caten/nn
      • Activation
      • Convolution
      • Criterion
      • Embedding
      • Linear
      • Normalization
      • Padding
      • Pooling
      • Encoding
      • Optimizers
  • Ready to use packages
    • Overview
    • caten/apps.gpt2
  • External Packages
    • caten/gguf
    • caten/oonx
    • caten/llm
In this article
  • Facet API
    • [generic] change-facet
    • [macro] with-facet
    • [macro] with-facets

Facet API

  1. Caten Documentation
  2. API Reference
  3. caten/api
  4. Facet API
|
  • Share via

  •  Edit this article

Facet API

We are going to see how to access the Tensor as a lisp object.

[generic] change-facet

(change-facet obj direction)

change-facet converts obj to the data type specified by direction. During the conversion process, it attempts to synchronize the Buffer (i.e., no copying is performed).

By default, :direction could be one of :tensor, :simple-array, :array.

Users can extend this method if needed.

lisp
CATEN-USER> (change-facet 1 :tensor)
Result
Result
{Tensor{LISPBUFFER}[int64] :shape NIL :id STC339772
  1
  :op #<PROCEEDNODE {1004CBAD73}>
  :requires-grad NIL
  :variables (SID339763)
  :tracker #<TRACKER :order={rowNIL} :shape=() :contiguous-p=T>}
lisp
CATEN-USER> (change-facet '((1.0 2.0 3.0) (4.0 5.0 6.0)) :tensor)
Result
Result
{Tensor{LISPBUFFER}[float32] :shape (2 3) :id TID339773
   ((1.0 2.0 3.0)
    (4.0 5.0 6.0))
  :op #<ALLOCATE {1004CBB5D3}>
  :requires-grad NIL
  :variables NIL
  :tracker #<TRACKER :order={row(0 1)} :shape=(2 3) :contiguous-p=T>}
lisp
CATEN-USER> (change-facet #(1 2 3) :tensor)
Result
Result
{Tensor{LISPBUFFER}[int64] :shape (3) :id TID339777
   (1 2 3)
  :op #<ALLOCATE {1004CBB773}>
  :requires-grad NIL
  :variables NIL
  :tracker #<TRACKER :order={row(0)} :shape=(3) :contiguous-p=T>}
lisp
CATEN-USER> (change-facet (rand `(2 2)) :simple-array)
Result
Result
#(0.60825 0.5240749 0.87959164 0.27363405)
lisp
CATEN-USER> (change-facet (rand `(2 2)) :array)
Result
Result
#2A((0.7030305 0.46698764) (0.40553668 0.9920771))

[macro] with-facet

(with-facet (bind (object &key (direction :array))) &body body)

Binds the result of (change-facet object direction) to the bind.

[macro] with-facets

(with-facets ((&rest input-forms) &body body))

Expands to a series of with-facet forms.

If you want to access an individual element of Tensor, it is wiser to convert it into an Array. The following code snippet initializes the diagonal of a Tensor to 0.0 without creating a copy:

lisp
CATEN-USER> (let ((x (rand `(3 3))))
  (with-facet (a (x :direction :array))
    (setf (aref a 0 0) 0.0
          (aref a 1 1) 0.0
          (aref a 2 2) 0.0))
  (print x))
Result
Result
{Tensor{LISPBUFFER}[float32] :shape (3 3) :id TID339791
   ((0.0        0.41170076 0.84356534)
    (0.72477406 0.0        0.9174599)
    (0.46578503 0.55132806 0.0))
  :op #<ALLOCATE {1004EC30C3}>
  :requires-grad NIL
  :variables NIL
  :tracker #<TRACKER :order={row(0 1)} :shape=(3 3) :contiguous-p=T>}
Search
Enter a keyword to search.