Skip to content

Commit

Permalink
[WIP] Only the well tested opset converter alives in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
hikettei committed Jun 11, 2024
1 parent da3921c commit 78a5396
Showing 1 changed file with 0 additions and 189 deletions.
189 changes: 0 additions & 189 deletions frontends/onnx/opset.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@
(data (nth 0 inputs))
(indices (nth 1 inputs)))
;; WIP
(warn "Gather implementation may not be complete")
;;(print axis)
;;(print indices)
;;(print data)
Expand Down Expand Up @@ -440,194 +439,6 @@
(nth i (wf/t:shape (car inputs)))))))))
(apply #'wf:!reshape (car inputs) reshaped))))

(wf/nodes:defnode
(Resize2DLinear (self in-shape ndims output-shape)
:where (data[in-shape] X-out[ndims] Y-out[ndims] out-to[output-shape] -> out-to[output-shape])
:slots ((out-shape :initarg :output-shape :initform nil :accessor resize-out-shape-of)))
(setf (wf/nodes:ignore-shape-error self) t))

(wf/nodes:define-impl-op
(Resize2DLinear)
:forward ((self data x-out y-out out-to)
(print "RESIZE2D")
;; [TODO] Optimize
;; [TODO] Rewrite this kernel using AbstractTensor
;; [TODO] Move this operation into wf/nn or wf/base-impl or wf/frontends/externels
(let ((ret))
(loop for x across (change-facet x-out :direction 'simple-array) do
(loop for y across (change-facet y-out :direction 'simple-array)
for x-floor = (floor x)
for y-floor = (floor y)
for x-ceil = (1+ (ceiling x))
for y-ceil = (1+ (ceiling y))
for wx = (- (ceiling x) x)
for wy = (- (ceiling y) y)
if (and (= x x-floor) (= y y-floor))
do (setf ret (append ret (list 0)))
else if (= x x-floor)
do (setf ret (append ret (list 0)))
else if (= y y-floor)
do (setf ret (append ret (list 0)))
else do (setf ret (append ret (list 0)))))
(wf:proceed
(apply
#'wf:!reshape
(change-facet ret :direction 'AbstractTensor)
(map 'list #'wf/vm:maybe-observe-axis (resize-out-shape-of self)))))))


(defun resize-op-11-13-common (cls size inputs attrs)
(let* ((roi (nth 1 inputs))
(roi (if (and roi (= (nth 0 (wf/t:shape roi)) 0))
nil
roi))
(ndims (wf/t:dims (car inputs)))
(mode (if (gethash "mode" attrs)
(map 'string #'code-char (gethash "mode" attrs))
"nearest"))
(method
(macrolet ((of (value) `(string= mode ,value)))
(cond
((of "nearest") "nearest_neighbor")
((of "linear") "linear")
((of "cubic") "cubic")
(T (error "~a is not valid." mode)))))
(coord-trans
(if (gethash "coordinate_transformation_mode" attrs)
(map 'string #'code-char (gethash "coordinate_transformation_mode" attrs))
"half_pixel"))
(nearest-mode
(if (gethash "nearest_mode" attrs)
(map 'string #'code-char (gethash "nearest_mode" attrs))
"round_prefer_floor"))
(alpha (gethash "cubic_coeff_a" attrs -0.75))
(exclude (gethash "exclude_outside" attrs 0))
(extrapolation-value (gethash "extrapolation_value" attrs 0.0))
(roi (when roi (error "resize-op-11-13-common: roi=True is not implemented yet.")))
(out-size (if size size (error "size=nil is not impllemented yet.")))
(out-size-lazy-list
(loop for i upfrom 0 below ndims
collect
(wf/vm:make-lazyaxis `(wf/t:vref ,out-size ,i)))))
(declare (type wf/t:AbstractTensor out-size))

(labels
((nearest-gather (X x-out y-out)
(wf:!view (wf:!view x t t y-out t) t t t x-out))
(nearest-mode (x-resized x-len)
(macrolet ((of (value) `(string= nearest-mode ,value)))
(cond
((of "round_prefer_ceil") (wf:!- x-len 0.5) (error "round_prefer_floor"))
((of "round_prefer_floor") (wf:lazy #'floor (wf:!- x-len 0.5)))
((of "floor") (wf/vm:make-lazyaxis `(floor ,x-len)))
((of "ceil") (error "ceil"))
(T (error "unknown nearest-mode ~a" nearest-mode)))))
(coordinate-trans (x-out y-out out-shape scales)
(macrolet ((of (value) `(string= coord-trans ,value)))
(multiple-value-bind (x-out y-out)
(cond
((of "half_pixel")
(values
(wf:!- (wf:!/ (wf:!+ 0.5 x-out) (wf:!view (wf:!view scales 3) (wf:broadcast-to x-out))) 0.5)
(wf:!- (wf:!/ (wf:!+ 0.5 y-out) (wf:!view (wf:!view scales 2) (wf:broadcast-to y-out))) 0.5)))
((of "asymmetric")
(values
(wf:!/ x-out (wf:!- (wf:!view scales 0)))
(wf:!/ y-out (wf:!- (wf:!view scales 1)))))
(T
(error "coordinate-trans: ~a is not implemented" coord-trans)))
(values x-out y-out)))))
(let ((x-out (wf:lazy-index-components #'(lambda (i) i) (make-input `(,(wf/vm:make-lazyaxis `(wf/t:vref ,out-size ,(+ ndims -1)))) nil)))
(y-out (wf:lazy-index-components #'(lambda (i) i) (make-input `(,(wf/vm:make-lazyaxis `(wf/t:vref ,out-size ,(+ ndims -2)))) nil))))
(case ndims
(4
(macrolet ((of (value) `(string= mode ,value)))
(cond
((of "nearest")
(multiple-value-bind (x-out y-out)
(coordinate-trans x-out y-out out-size size)
(nearest-gather
(car inputs)
(nearest-mode x-out (car (last (wf/t:shape (car inputs)))))
(nearest-mode y-out (car (last (wf/t:shape (car inputs))))))))
((of "linear")
(multiple-value-bind (x-out y-out)
(coordinate-trans x-out y-out out-size size)
;; x-out/y-out [size] Tensor
;; lambda f de dounika suru
(wf/nodes:call
(Resize2DLinear
(wf/t:shape (car inputs))
ndims
out-size-lazy-list)
(car inputs)
x-out
y-out
(make-input out-size-lazy-list nil))
;;(error "")
))
(T (error "~a is not implemented" mode)))))
(T (error "resize-op-11-13-common: ndims = ~a is not implemented" ndims)))))))

(defop ("Resize" 11)
((cls inputs attrs)
(let* ((scale (nth 2 inputs))
(size (if (= (length inputs) 4)
(nth 3 inputs)
(wf:!mul (nth 0 inputs) (wf:!flexible scale)))))
(car (multiple-value-list (resize-op-11-13-common cls size inputs attrs))))))

(defop ("Resize" 13)
((cls inputs attrs)
(let* ((scale (nth 2 inputs))
(size (nth 3 inputs)))
(if size
(assert (null scale))
(error "NOT IMPLEMENTED"))
(car (multiple-value-list (resize-op-11-13-common cls size inputs attrs))))))


#|
def non_max_suppression(
predictions: np.ndarray, iou_threshold: float = 0.5
) -> np.ndarray:
rows, columns = predictions.shape
sort_index = np.flip(predictions[:, 4].argsort())
predictions = predictions[sort_index]
boxes = predictions[:, :4]
categories = predictions[:, 5]
ious = box_iou_batch(boxes, boxes)
ious = ious - np.eye(rows)
keep = np.ones(rows, dtype=bool)
for index, (iou, category) in enumerate(zip(ious, categories)):
if not keep[index]:
continue
condition = (iou > iou_threshold) & (categories == category)
keep = keep & ~condition
return keep[sort_index.argsort()]
|#
(defop ("NonMaxSuppression" 11)
((cls inputs attr)
(warn "NonMaxSupression is not implemented yet.")
(let* ((boxes (car inputs))
(scores (second inputs))
(mobpe (third inputs))
(iou-threshold (fourth inputs))
(sth (fifth inputs)))

(print boxes)
(print scores)
(print mobpe)
(print iou-threshold)
(print sth)
(wf:!add boxes scores))))

(defop ("GlobalAveragePool" 1)
((cls inputs attrs)
(declare (ignore attrs))
Expand Down

0 comments on commit 78a5396

Please sign in to comment.