You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by cm...@apache.org on 2019/01/20 18:45:37 UTC

[incubator-mxnet] branch clojure-resource-scope updated: clean up and add tests

This is an automated email from the ASF dual-hosted git repository.

cmeier pushed a commit to branch clojure-resource-scope
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/clojure-resource-scope by this push:
     new f8c0768  clean up and add tests
f8c0768 is described below

commit f8c07688954af72d1b2fa0aa14c6b9277a06488d
Author: gigasquid <cm...@gigasquidsoftware.com>
AuthorDate: Sun Jan 20 13:45:12 2019 -0500

    clean up and add tests
---
 .../org/apache/clojure_mxnet/resource_scope.clj    | 22 ++++------------------
 .../src/org/apache/clojure_mxnet/util.clj          |  6 ++++++
 .../apache/clojure_mxnet/resource_scope_test.clj   | 19 +++++++++++++++++++
 .../test/org/apache/clojure_mxnet/util_test.clj    |  7 +++++++
 4 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/resource_scope.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/resource_scope.clj
index 75c7f76..e7dea84 100644
--- a/contrib/clojure-package/src/org/apache/clojure_mxnet/resource_scope.clj
+++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/resource_scope.clj
@@ -1,16 +1,10 @@
 (ns org.apache.clojure-mxnet.resource-scope
-  (:require [org.apache.clojure-mxnet.util :as util]
-            [t6.from-scala.core :refer [$ $$] :as $]
-            [org.apache.clojure-mxnet.ndarray :as ndarray])
+  (:require [org.apache.clojure-mxnet.util :as util])
   (:import (org.apache.mxnet ResourceScope)))
 
-(require '[clojure.reflect :as r])
-
-(defmacro forms->scala-fn
-  "Creates a scala fn of zero args from forms"
-  [& forms]
-  `($/fn []
-     (do ~@forms)))
+;; Captures all Native Resources created using the ResourceScope and
+;; at the end of the body, de allocates all the Native resources by calling close on them.
+;;This method will not deAllocate NativeResources returned from the forms.
 
 (defmacro using
   "Uses a Resource Scope for all forms"
@@ -25,11 +19,3 @@
           z {:just-a-test (def temp-z (ndarray/ones [3 3]))}
           y (ndarray/+ temp-x 1)]
       y))))
-
-(comment
-  (.isDisposed temp-x)
-  (.isDisposed temp-y)
-  (.isDisposed temp-z)
-  (.isDisposed return-val)
-  (ndarray/->vec return-val))
-
diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/util.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/util.clj
index 43970c0..6b5f507 100644
--- a/contrib/clojure-package/src/org/apache/clojure_mxnet/util.clj
+++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/util.clj
@@ -239,3 +239,9 @@
            (apply $/immutable-list))
       ;; pass-through
       map-or-tuple-seq)))
+
+(defmacro forms->scala-fn
+  "Creates a scala fn of zero args from forms"
+  [& forms]
+  `($/fn []
+     (do ~@forms)))
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/resource_scope_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/resource_scope_test.clj
new file mode 100644
index 0000000..dd1024c
--- /dev/null
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/resource_scope_test.clj
@@ -0,0 +1,19 @@
+(ns org.apache.clojure-mxnet.resource-scope-test
+  (:require [org.apache.clojure-mxnet.ndarray :as ndarray]
+            [org.apache.clojure-mxnet.resource-scope :as resource-scope]
+            [clojure.test :refer :all]))
+
+(deftest test-resource-scope-with-ndarray
+  (let [x (ndarray/ones [2 2])
+        return-val (resource-scope/using
+                    (def temp-x (ndarray/ones [3 1]))
+                    (def temp-y (ndarray/ones [3 1]))
+                    (let [z {:just-a-test (def temp-z (ndarray/ones [3 3]))}
+                          y (ndarray/+ temp-x 1)]
+                      y))]
+    (is (true? (.isDisposed temp-x)))
+    (is (true? (.isDisposed temp-y)))
+    (is (true? (.isDisposed temp-z)))
+    (is (false? (.isDisposed return-val)))
+    (is (false? (.isDisposed x)))
+    (is (= [2.0 2.0 2.0] (ndarray/->vec return-val)))))
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/util_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/util_test.clj
index c26f83d..4ed7d38 100644
--- a/contrib/clojure-package/test/org/apache/clojure_mxnet/util_test.clj
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/util_test.clj
@@ -226,3 +226,10 @@
   (let [nda (util/map->scala-tuple-seq {:a-b (ndarray/ones [1 2])})]
     (is (= "a_b" (._1 (.head nda))))
     (is (= [1.0 1.0] (ndarray/->vec (._2 (.head nda)))))))
+
+(deftest test-forms->scala-fn
+  (let [scala-fn (util/forms->scala-fn
+                  (def x 1)
+                  (def y 2)
+                  {:x x :y y})]
+    (is (= {:x 1 :y 2} (.apply scala-fn)))))