This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add primitives support handling to the generator for proper conversion
- Loading branch information
1 parent
1d9b8bb
commit d5595a0
Showing
5 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
contrib/clojure-package/src/org/apache/clojure_mxnet/primitives.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(ns org.apache.clojure-mxnet.primitives | ||
(:import (org.apache.mxnet MX_PRIMITIVES$MX_FLOAT MX_PRIMITIVES$MX_Double | ||
MX_PRIMITIVES$MX_PRIMITIVE_TYPE))) | ||
|
||
|
||
;;; Defines customer mx primitives that can be used for mathematical computations | ||
;;; in NDArrays to control precision. Currently Float and Double are supported | ||
|
||
;;; For purposes of automatic conversion in ndarray functions, doubles are default | ||
;; to specify using floats you must use a Float | ||
|
||
(defn mx-float | ||
"Creates a MXNet float primitive" | ||
[num] | ||
(new MX_PRIMITIVES$MX_FLOAT num)) | ||
|
||
(defn mx-double | ||
"Creates a MXNet double primitive" | ||
[num] | ||
(new MX_PRIMITIVES$MX_Double num)) | ||
|
||
(defn ->num | ||
"Returns the underlying number value" | ||
[primitive] | ||
(.data primitive)) | ||
|
||
(defn primitive? [x] | ||
(instance? MX_PRIMITIVES$MX_PRIMITIVE_TYPE x)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
contrib/clojure-package/test/org/apache/clojure_mxnet/primitives_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(ns org.apache.clojure-mxnet.primitives-test | ||
(:require [org.apache.clojure-mxnet.primitives :as primitives] | ||
[clojure.test :refer :all]) | ||
(:import (org.apache.mxnet MX_PRIMITIVES$MX_PRIMITIVE_TYPE | ||
MX_PRIMITIVES$MX_FLOAT | ||
MX_PRIMITIVES$MX_Double))) | ||
|
||
(deftest test-primitive-types | ||
(is (not (primitives/primitive? 3))) | ||
(is (primitives/primitive? (primitives/mx-float 3))) | ||
(is (primitives/primitive? (primitives/mx-double 3)))) | ||
|
||
(deftest test-float-primitives | ||
(is (instance? MX_PRIMITIVES$MX_PRIMITIVE_TYPE (primitives/mx-float 3))) | ||
(is (instance? MX_PRIMITIVES$MX_FLOAT (primitives/mx-float 3))) | ||
(is (instance? Float (-> (primitives/mx-float 3) | ||
(primitives/->num)))) | ||
(is (= 3.0 (-> (primitives/mx-float 3) | ||
(primitives/->num))))) | ||
|
||
(deftest test-double-primitives | ||
(is (instance? MX_PRIMITIVES$MX_PRIMITIVE_TYPE (primitives/mx-double 2))) | ||
(is (instance? MX_PRIMITIVES$MX_Double (primitives/mx-double 2))) | ||
(is (instance? Double (-> (primitives/mx-double 2) | ||
(primitives/->num)))) | ||
(is (= 2.0 (-> (primitives/mx-double 2) | ||
(primitives/->num))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters