forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Clojure] Add resource scope to clojure package (apache#13993)
* Add resource scope to clojure package * add rat * fix integration test * feedback from @benkamphaus - move from defs to atoms to make the tests a bit better * adding alias with-do and with-let more tests * another test * Add examples in docstring * refactor example and test to use resource-scope/with-let * fix tests and problem with laziness now they work as expected! * refactor to be a bit more modular * remove comments
- Loading branch information
Showing
6 changed files
with
252 additions
and
38 deletions.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
contrib/clojure-package/src/org/apache/clojure_mxnet/resource_scope.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,53 @@ | ||
;; | ||
;; Licensed to the Apache Software Foundation (ASF) under one or more | ||
;; contributor license agreements. See the NOTICE file distributed with | ||
;; this work for additional information regarding copyright ownership. | ||
;; The ASF licenses this file to You under the Apache License, Version 2.0 | ||
;; (the "License"); you may not use this file except in compliance with | ||
;; the License. You may obtain a copy of the License at | ||
;; | ||
;; http://www.apache.org/licenses/LICENSE-2.0 | ||
;; | ||
;; Unless required by applicable law or agreed to in writing, software | ||
;; distributed under the License is distributed on an "AS IS" BASIS, | ||
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
;; See the License for the specific language governing permissions and | ||
;; limitations under the License. | ||
;; | ||
|
||
(ns org.apache.clojure-mxnet.resource-scope | ||
(:require [org.apache.clojure-mxnet.util :as util]) | ||
(:import (org.apache.mxnet ResourceScope))) | ||
|
||
(defmacro | ||
using | ||
"Uses a Resource Scope for all forms. This is a way to manage all Native Resources like NDArray and Symbol - it will deallocate all Native Resources by calling close on them automatically. It will not call close on Native Resources returned from the form. | ||
Example: | ||
(resource-scope/using | ||
(let [temp-x (ndarray/ones [3 1]) | ||
temp-y (ndarray/ones [3 1])] | ||
(ndarray/+ temp-x temp-y))) " | ||
[& forms] | ||
`(ResourceScope/using (new ResourceScope) (util/forms->scala-fn ~@forms))) | ||
|
||
|
||
(defmacro | ||
with-do | ||
"Alias for a do within a resource scope using. | ||
Example: | ||
(resource-scope/with-do | ||
(ndarray/ones [3 1]) | ||
:all-cleaned-up) | ||
" | ||
[& forms] | ||
`(using (do ~@forms))) | ||
|
||
(defmacro | ||
with-let | ||
"Alias for a let within a resource scope using. | ||
Example: | ||
(resource-scope/with-let [temp-x (ndarray/ones [3 1]) | ||
temp-y (ndarray/ones [3 1])] | ||
(ndarray/+ temp-x temp-y))" | ||
[& forms] | ||
`(using (let ~@forms))) |
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
146 changes: 146 additions & 0 deletions
146
contrib/clojure-package/test/org/apache/clojure_mxnet/resource_scope_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,146 @@ | ||
;; | ||
;; Licensed to the Apache Software Foundation (ASF) under one or more | ||
;; contributor license agreements. See the NOTICE file distributed with | ||
;; this work for additional information regarding copyright ownership. | ||
;; The ASF licenses this file to You under the Apache License, Version 2.0 | ||
;; (the "License"); you may not use this file except in compliance with | ||
;; the License. You may obtain a copy of the License at | ||
;; | ||
;; http://www.apache.org/licenses/LICENSE-2.0 | ||
;; | ||
;; Unless required by applicable law or agreed to in writing, software | ||
;; distributed under the License is distributed on an "AS IS" BASIS, | ||
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
;; See the License for the specific language governing permissions and | ||
;; limitations under the License. | ||
;; | ||
|
||
(ns org.apache.clojure-mxnet.resource-scope-test | ||
(:require [org.apache.clojure-mxnet.ndarray :as ndarray] | ||
[org.apache.clojure-mxnet.symbol :as sym] | ||
[org.apache.clojure-mxnet.resource-scope :as resource-scope] | ||
[clojure.test :refer :all])) | ||
|
||
|
||
(deftest test-resource-scope-with-ndarray | ||
(let [native-resources (atom {}) | ||
x (ndarray/ones [2 2]) | ||
return-val (resource-scope/using | ||
(let [temp-x (ndarray/ones [3 1]) | ||
temp-y (ndarray/ones [3 1])] | ||
(swap! native-resources assoc :temp-x temp-x) | ||
(swap! native-resources assoc :temp-y temp-y) | ||
(ndarray/+ temp-x 1)))] | ||
(is (true? (ndarray/is-disposed (:temp-x @native-resources)))) | ||
(is (true? (ndarray/is-disposed (:temp-y @native-resources)))) | ||
(is (false? (ndarray/is-disposed return-val))) | ||
(is (false? (ndarray/is-disposed x))) | ||
(is (= [2.0 2.0 2.0] (ndarray/->vec return-val))))) | ||
|
||
(deftest test-nested-resource-scope-with-ndarray | ||
(let [native-resources (atom {}) | ||
x (ndarray/ones [2 2]) | ||
return-val (resource-scope/using | ||
(let [temp-x (ndarray/ones [3 1])] | ||
(swap! native-resources assoc :temp-x temp-x) | ||
(resource-scope/using | ||
(let [temp-y (ndarray/ones [3 1])] | ||
(swap! native-resources assoc :temp-y temp-y)))))] | ||
(is (true? (ndarray/is-disposed (:temp-y @native-resources)))) | ||
(is (true? (ndarray/is-disposed (:temp-x @native-resources)))) | ||
(is (false? (ndarray/is-disposed x))))) | ||
|
||
(deftest test-resource-scope-with-sym | ||
(let [native-resources (atom {}) | ||
x (sym/ones [2 2]) | ||
return-val (resource-scope/using | ||
(let [temp-x (sym/ones [3 1]) | ||
temp-y (sym/ones [3 1])] | ||
(swap! native-resources assoc :temp-x temp-x) | ||
(swap! native-resources assoc :temp-y temp-y) | ||
(sym/+ temp-x 1)))] | ||
(is (true? (sym/is-disposed (:temp-x @native-resources)))) | ||
(is (true? (sym/is-disposed (:temp-y @native-resources)))) | ||
(is (false? (sym/is-disposed return-val))) | ||
(is (false? (sym/is-disposed x))))) | ||
|
||
(deftest test-nested-resource-scope-with-ndarray | ||
(let [native-resources (atom {}) | ||
x (ndarray/ones [2 2]) | ||
return-val (resource-scope/using | ||
(let [temp-x (ndarray/ones [3 1])] | ||
(swap! native-resources assoc :temp-x temp-x) | ||
(resource-scope/using | ||
(let [temp-y (ndarray/ones [3 1])] | ||
(swap! native-resources assoc :temp-y temp-y)))))] | ||
(is (true? (ndarray/is-disposed (:temp-y @native-resources)))) | ||
(is (true? (ndarray/is-disposed (:temp-x @native-resources)))) | ||
(is (false? (ndarray/is-disposed x))))) | ||
|
||
(deftest test-nested-resource-scope-with-sym | ||
(let [native-resources (atom {}) | ||
x (sym/ones [2 2]) | ||
return-val (resource-scope/using | ||
(let [temp-x (sym/ones [3 1])] | ||
(swap! native-resources assoc :temp-x temp-x) | ||
(resource-scope/using | ||
(let [temp-y (sym/ones [3 1])] | ||
(swap! native-resources assoc :temp-y temp-y)))))] | ||
(is (true? (sym/is-disposed (:temp-y @native-resources)))) | ||
(is (true? (sym/is-disposed (:temp-x @native-resources)))) | ||
(is (false? (sym/is-disposed x))))) | ||
|
||
(deftest test-list-creation-with-returning-first | ||
(let [native-resources (atom []) | ||
return-val (resource-scope/using | ||
(let [temp-ndarrays (doall (repeatedly 3 #(ndarray/ones [3 1]))) | ||
_ (reset! native-resources temp-ndarrays)] | ||
(first temp-ndarrays)))] | ||
(is (false? (ndarray/is-disposed return-val))) | ||
(is (= [false true true] (mapv ndarray/is-disposed @native-resources))))) | ||
|
||
(deftest test-list-creation | ||
(let [native-resources (atom []) | ||
return-val (resource-scope/using | ||
(let [temp-ndarrays (doall (repeatedly 3 #(ndarray/ones [3 1]))) | ||
_ (reset! native-resources temp-ndarrays)] | ||
(ndarray/ones [3 1])))] | ||
(is (false? (ndarray/is-disposed return-val))) | ||
(is (= [true true true] (mapv ndarray/is-disposed @native-resources))))) | ||
|
||
(deftest test-list-creation-without-let | ||
(let [native-resources (atom []) | ||
return-val (resource-scope/using | ||
(first (doall (repeatedly 3 #(do | ||
(let [x (ndarray/ones [3 1])] | ||
(swap! native-resources conj x) | ||
x))))))] | ||
(is (false? (ndarray/is-disposed return-val))) | ||
(is (= [false true true] (mapv ndarray/is-disposed @native-resources))))) | ||
|
||
(deftest test-with-let | ||
(let [native-resources (atom {}) | ||
x (ndarray/ones [2 2]) | ||
return-val (resource-scope/with-let [temp-x (ndarray/ones [3 1]) | ||
temp-y (ndarray/ones [3 1])] | ||
(swap! native-resources assoc :temp-x temp-x) | ||
(swap! native-resources assoc :temp-y temp-y) | ||
(ndarray/+ temp-x 1))] | ||
(is (true? (ndarray/is-disposed (:temp-x @native-resources)))) | ||
(is (true? (ndarray/is-disposed (:temp-y @native-resources)))) | ||
(is (false? (ndarray/is-disposed return-val))) | ||
(is (false? (ndarray/is-disposed x))) | ||
(is (= [2.0 2.0 2.0] (ndarray/->vec return-val))))) | ||
|
||
(deftest test-with-do | ||
(let [native-resources (atom {}) | ||
x (ndarray/ones [2 2]) | ||
return-val (resource-scope/with-do | ||
(swap! native-resources assoc :temp-x (ndarray/ones [3 1])) | ||
(swap! native-resources assoc :temp-y (ndarray/ones [3 1])) | ||
(ndarray/ones [3 1]))] | ||
(is (true? (ndarray/is-disposed (:temp-x @native-resources)))) | ||
(is (true? (ndarray/is-disposed (:temp-y @native-resources)))) | ||
(is (false? (ndarray/is-disposed return-val))) | ||
(is (false? (ndarray/is-disposed x))) | ||
(is (= [1.0 1.0 1.0] (ndarray/->vec return-val))))) |
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