From 8882a0c0f8b5c02f6e69e8301f06ed0044e83f27 Mon Sep 17 00:00:00 2001 From: Chouffe Date: Fri, 19 Apr 2019 19:26:55 +0200 Subject: [PATCH 1/4] [clojure][generator] add random namespace generation * `ndarray_random_api` * `symbol_random_api` --- contrib/clojure-package/src/dev/generator.clj | 113 +++++++++++++++++- .../clojure_mxnet/ndarray_random_api.clj | 12 ++ 2 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_random_api.clj diff --git a/contrib/clojure-package/src/dev/generator.clj b/contrib/clojure-package/src/dev/generator.clj index 34210bef63d0..de4a333c7d36 100644 --- a/contrib/clojure-package/src/dev/generator.clj +++ b/contrib/clojure-package/src/dev/generator.clj @@ -126,6 +126,12 @@ (clojure.pprint/pprint f w) (.write w "\n")))) +(defn remove-prefix + [prefix s] + (let [regex (re-pattern (str prefix "(.*)")) + replacement "$1"] + (clojure.string/replace s regex replacement))) + ;;;;;;; Common operations (def libinfo (Base/_LIB)) @@ -470,6 +476,53 @@ (println "Generating symbol-api file") (write-to-file all-symbol-api-functions symbol-api-gen-ns "src/org/apache/clojure_mxnet/gen/symbol_api.clj")) +;;;;;;; SymbolRandomAPI + +(defn gen-symbol-random-api-function [op-name] + (let [{:keys [fn-name fn-description args]} (gen-op-info op-name) + fn-name (remove-prefix "-random-" fn-name) + params (mapv (fn [{:keys [name type optional?] :as opts}] + (assoc opts + :sym (symbol name) + :optional? (or optional? + (= "NDArray-or-Symbol" type)))) + (conj args + {:name "name" + :type "String" + :optional? true + :description "Name of the symbol"} + {:name "attr" + :type "Map[String, String]" + :optional? true + :description "Attributes of the symbol"})) + doc (gen-symbol-api-doc fn-description params) + default-call (gen-symbol-api-default-arity op-name params)] + `(~'defn ~(symbol fn-name) + ~doc + ~@default-call))) + +(def all-symbol-random-api-functions + (->> op-names + (filter #(clojure.string/includes? % "random_")) + (mapv gen-symbol-random-api-function))) + +(def symbol-random-api-gen-ns "(ns + ^{:doc \"Experimental\"} + org.apache.clojure-mxnet.symbol-random-api + (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max + min repeat reverse set sort take to-array empty sin + get apply shuffle ref]) + (:require [org.apache.clojure-mxnet.util :as util] + [org.apache.clojure-mxnet.shape :as mx-shape]) + (:import (org.apache.mxnet SymbolAPI)))") + +(defn generate-symbol-random-api-file [] + (println "Generating symbol-random-api file") + (write-to-file all-symbol-random-api-functions + symbol-random-api-gen-ns + "src/org/apache/clojure_mxnet/gen/symbol_random_api.clj")) + + ;;;;;;; NDArrayAPI (defn ndarray-api-coerce-param @@ -535,7 +588,9 @@ ~default-call)))) (def all-ndarray-api-functions - (mapv gen-ndarray-api-function op-names)) + (->> op-names + (remove #(clojure.string/includes? % "random_")) + (mapv gen-ndarray-api-function))) (def ndarray-api-gen-ns "(ns ^{:doc \"Experimental\"} @@ -554,12 +609,60 @@ ndarray-api-gen-ns "src/org/apache/clojure_mxnet/gen/ndarray_api.clj")) +;;;;;;; NDArrayRandomAPI + +(defn gen-ndarray-random-api-function [op-name] + (let [{:keys [fn-name fn-description args]} (gen-op-info op-name) + fn-name (remove-prefix "-random-" fn-name) + params (mapv (fn [{:keys [name] :as opts}] + (assoc opts :sym (symbol name))) + (conj args {:name "out" + :type "NDArray-or-Symbol" + :optional? true + :description "Output array."})) + doc (gen-ndarray-api-doc fn-description params) + opt-params (filter :optional? params) + req-params (remove :optional? params) + req-call (gen-ndarray-api-required-arity fn-name req-params) + default-call (gen-ndarray-api-default-arity op-name params)] + (if (= 1 (count req-params)) + `(~'defn ~(symbol fn-name) + ~doc + ~@default-call) + `(~'defn ~(symbol fn-name) + ~doc + ~req-call + ~default-call)))) + +(def all-ndarray-random-api-functions + (->> op-names + (filter #(clojure.string/includes? % "random_")) + (mapv gen-ndarray-random-api-function))) + +(def ndarray-random-api-gen-ns "(ns + ^{:doc \"Experimental\"} + org.apache.clojure-mxnet.ndarray-random-api + (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max + min repeat reverse set sort take to-array empty shuffle + ref]) + (:require [org.apache.clojure-mxnet.shape :as mx-shape] + [org.apache.clojure-mxnet.util :as util]) + (:import (org.apache.mxnet NDArrayAPI)))") + +(defn generate-ndarray-random-api-file [] + (println "Generating ndarray-random-api file") + (write-to-file all-ndarray-random-api-functions + ndarray-random-api-gen-ns + "src/org/apache/clojure_mxnet/gen/ndarray_random_api.clj")) + ;;; autogen the files (do (generate-ndarray-file) (generate-ndarray-api-file) + (generate-ndarray-random-api-file) (generate-symbol-file) - (generate-symbol-api-file)) + (generate-symbol-api-file) + (generate-symbol-random-api-file)) (comment @@ -570,8 +673,12 @@ (gen-symbol-api-function "Activation") + (gen-ndarray-random-api-function "random_randint") + + (gen-symbol-random-api-function "random_poisson") + ;; This generates a file with the bulk of the nd-array functions (generate-ndarray-file) ;; This generates a file with the bulk of the symbol functions - (generate-symbol-file) ) + (generate-symbol-file)) diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_random_api.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_random_api.clj new file mode 100644 index 000000000000..6027bde7ed7a --- /dev/null +++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_random_api.clj @@ -0,0 +1,12 @@ +(ns org.apache.clojure-mxnet.ndarray-random-api + "Experimental NDArray Random API" + (:require [org.apache.clojure-mxnet.base :as base] + [org.apache.clojure-mxnet.context :as mx-context] + [org.apache.clojure-mxnet.shape :as mx-shape] + [org.apache.clojure-mxnet.util :as util] + [clojure.reflect :as r] + [t6.from-scala.core :refer [$] :as $]) + (:import (org.apache.mxnet NDArrayAPI))) + +;; loads the generated functions into the namespace +(do (clojure.core/load "gen/ndarray_random_api")) From c49d5d387e8102539bb600fa593b3bcd5980eac5 Mon Sep 17 00:00:00 2001 From: Chouffe Date: Wed, 24 Apr 2019 16:02:58 +0200 Subject: [PATCH 2/4] finish ndarray api random and sample file generation --- contrib/clojure-package/src/dev/generator.clj | 287 ++++++++++-------- .../clojure_mxnet/ndarray_sample_api.clj | 12 + .../test/dev/generator_test.clj | 24 +- 3 files changed, 194 insertions(+), 129 deletions(-) create mode 100644 contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_sample_api.clj diff --git a/contrib/clojure-package/src/dev/generator.clj b/contrib/clojure-package/src/dev/generator.clj index de4a333c7d36..339943500ea7 100644 --- a/contrib/clojure-package/src/dev/generator.clj +++ b/contrib/clojure-package/src/dev/generator.clj @@ -29,6 +29,7 @@ (defn clojure-case + "Transforms a scale string (function name) to clojure case" [string] (-> string (clojure.string/replace #"(\s+)([A-Z][a-z]+)" "$1-$2") @@ -57,10 +58,9 @@ count pos?)) - (defn increment-param-name [pname] (if-let [num-str (re-find #"-\d" pname)] - (str + (str (first (clojure.string/split pname #"-")) "-" (inc (Integer/parseInt (last (clojure.string/split num-str #"-"))))) @@ -132,9 +132,27 @@ replacement "$1"] (clojure.string/replace s regex replacement))) +(defn op-name->namespace + [op-name] + (cond + (#{"uniform" "normal"} op-name) :deprecated + (clojure.string/starts-with? op-name "random_") :random + (clojure.string/starts-with? op-name "sample_") :sample + :else :core)) + +(op-name->namespace "random_uniform") +(op-name->namespace "sample_uniform") +(op-name->namespace "activation") + +(defn in-namespace-random? [op-name] + (or (#{"uniform" "normal"} op-name) + (clojure.string/includes? op-name "random_") + (clojure.string/includes? op-name "sample_"))) + ;;;;;;; Common operations (def libinfo (Base/_LIB)) + (def op-names (let [l ($ ListBuffer/empty)] (do (.mxListAllOpNames libinfo l) @@ -290,8 +308,6 @@ `(~'defn ~function-name ~@(remove nil? (gen-symbol-function-arity op-name op-values function-name)))))) - - (def symbol-gen-ns "(ns org.apache.clojure-mxnet.symbol (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max min repeat reverse set sort take to-array empty sin @@ -302,7 +318,9 @@ (defn generate-symbol-file [] (println "Generating symbol file") - (write-to-file all-symbol-functions symbol-gen-ns "src/org/apache/clojure_mxnet/gen/symbol.clj")) + (write-to-file all-symbol-functions + symbol-gen-ns + "src/org/apache/clojure_mxnet/gen/symbol.clj")) ;;;;;;; NDArray @@ -324,21 +342,17 @@ count pos?)) - (def ndarray-public-to-hand-gen (filter is-ndarray-hand-gen? ndarray-public-no-default)) (def ndarray-public-to-gen (get-public-to-gen-methods ndarray-public-to-hand-gen ndarray-public-no-default)) - (count ndarray-public-to-hand-gen) ;=> 15 (count ndarray-public-to-gen) ;=> 486 (->> ndarray-public-to-hand-gen (map :name) (into #{})) - - (defn gen-ndarray-function-arity [op-name op-values] (for [[param-count info] op-values] (let [targets (->> (mapv :parameter-types info) @@ -382,7 +396,8 @@ (def all-ndarray-functions (gen-ndarray-functions ndarray-public-to-gen)) -(def ndarray-gen-ns "(ns org.apache.clojure-mxnet.ndarray +(def ndarray-gen-ns + "(ns org.apache.clojure-mxnet.ndarray (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max min repeat reverse set sort take to-array empty shuffle ref]) @@ -459,22 +474,31 @@ ~doc ~@default-call))) -(def all-symbol-api-functions - (mapv gen-symbol-api-function op-names)) - -(def symbol-api-gen-ns "(ns - ^{:doc \"Experimental\"} - org.apache.clojure-mxnet.symbol-api - (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max - min repeat reverse set sort take to-array empty sin - get apply shuffle ref]) - (:require [org.apache.clojure-mxnet.util :as util] - [org.apache.clojure-mxnet.shape :as mx-shape]) - (:import (org.apache.mxnet SymbolAPI)))") +(defn all-symbol-api-functions [op-names] + (->> op-names + (remove in-namespace-random?) + (mapv gen-symbol-api-function))) -(defn generate-symbol-api-file [] +(defn symbol-api-gen-ns + [random?] + (str + "(ns\n" + " ^{:doc \"Experimental\"}\n" + " org.apache.clojure-mxnet.symbol-api\n" + " (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max\n" + " min repeat reverse set sort take to-array empty sin\n" + " get apply shuffle ref])\n" + " (:require [org.apache.clojure-mxnet.util :as util]\n" + " [org.apache.clojure-mxnet.shape :as mx-shape])\n" + (if random? + " (:import (org.apache.mxnet SymbolAPI SymbolRandomAPI)))" + " (:import (org.apache.mxnet SymbolAPI)))"))) + +(defn generate-symbol-api-file [op-names] (println "Generating symbol-api file") - (write-to-file all-symbol-api-functions symbol-api-gen-ns "src/org/apache/clojure_mxnet/gen/symbol_api.clj")) + (write-to-file (all-symbol-api-functions op-names) + (symbol-api-gen-ns false) + "src/org/apache/clojure_mxnet/gen/symbol_api.clj")) ;;;;;;; SymbolRandomAPI @@ -501,25 +525,15 @@ ~doc ~@default-call))) -(def all-symbol-random-api-functions +(defn all-symbol-random-api-functions [op-names] (->> op-names - (filter #(clojure.string/includes? % "random_")) + (filter in-namespace-random?) (mapv gen-symbol-random-api-function))) -(def symbol-random-api-gen-ns "(ns - ^{:doc \"Experimental\"} - org.apache.clojure-mxnet.symbol-random-api - (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max - min repeat reverse set sort take to-array empty sin - get apply shuffle ref]) - (:require [org.apache.clojure-mxnet.util :as util] - [org.apache.clojure-mxnet.shape :as mx-shape]) - (:import (org.apache.mxnet SymbolAPI)))") - -(defn generate-symbol-random-api-file [] +(defn generate-symbol-random-api-file [op-names] (println "Generating symbol-random-api file") - (write-to-file all-symbol-random-api-functions - symbol-random-api-gen-ns + (write-to-file (all-symbol-random-api-functions op-names) + (symbol-api-gen-ns true) "src/org/apache/clojure_mxnet/gen/symbol_random_api.clj")) @@ -565,104 +579,129 @@ `(~(mapv :sym req-params) (~(symbol fn-name) ~req-args)))) -(defn gen-ndarray-api-function [op-name] - (let [{:keys [fn-name fn-description args]} (gen-op-info op-name) - params (mapv (fn [{:keys [name] :as opts}] - (assoc opts :sym (symbol name))) - (conj args {:name "out" - :type "NDArray-or-Symbol" - :optional? true - :description "Output array."})) - doc (gen-ndarray-api-doc fn-description params) - opt-params (filter :optional? params) - req-params (remove :optional? params) - req-call (gen-ndarray-api-required-arity fn-name req-params) - default-call (gen-ndarray-api-default-arity op-name params)] - (if (= 1 (count req-params)) - `(~'defn ~(symbol fn-name) - ~doc - ~@default-call) - `(~'defn ~(symbol fn-name) - ~doc - ~req-call - ~default-call)))) - -(def all-ndarray-api-functions +(defn make-gen-ndarray-api-function + [{:keys [fn-name->fn-name] :or {fn-name->fn-name identity}}] + (fn [op-name] + (let [{:keys [fn-name fn-description args]} + (-> op-name (gen-op-info) (update :fn-name fn-name->fn-name)) + params (mapv (fn [{:keys [name] :as opts}] + (assoc opts :sym (symbol name))) + (conj args {:name "out" + :type "NDArray-or-Symbol" + :optional? true + :description "Output array."})) + doc (gen-ndarray-api-doc fn-description params) + opt-params (filter :optional? params) + req-params (remove :optional? params) + req-call (gen-ndarray-api-required-arity fn-name req-params) + default-call (gen-ndarray-api-default-arity op-name params)] + (if (= 1 (count req-params)) + `(~'defn ~(symbol fn-name) + ~doc + ~@default-call) + `(~'defn ~(symbol fn-name) + ~doc + ~req-call + ~default-call))))) + +(defn fn-name->random-fn-name + [fn-name] + (if (clojure.string/starts-with? fn-name "-random-") + (remove-prefix "-random-" fn-name) + fn-name)) + +(defn fn-name->sample-fn-name + [fn-name] + (if (clojure.string/starts-with? fn-name "-sample-") + (remove-prefix "-sample-" fn-name) + fn-name)) + +(fn-name->random-fn-name "-random-exponential") ;"exponential" +(fn-name->sample-fn-name "-sample-poisson") ;"poisson" + +(def gen-ndarray-api-function + (make-gen-ndarray-api-function {})) + +(def gen-ndarray-random-api-function + (make-gen-ndarray-api-function {:fn-name->fn-name fn-name->random-fn-name})) + +(def gen-ndarray-sample-api-function + (make-gen-ndarray-api-function {:fn-name->fn-name fn-name->sample-fn-name})) + +(defn all-ndarray-api-functions [op-names] (->> op-names - (remove #(clojure.string/includes? % "random_")) + (filter #(= :core (op-name->namespace %))) (mapv gen-ndarray-api-function))) -(def ndarray-api-gen-ns "(ns - ^{:doc \"Experimental\"} - org.apache.clojure-mxnet.ndarray-api - (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max - min repeat reverse set sort take to-array empty shuffle - ref]) - (:require [org.apache.clojure-mxnet.shape :as mx-shape] - [org.apache.clojure-mxnet.util :as util]) - (:import (org.apache.mxnet NDArrayAPI)))") - +(count (all-ndarray-api-functions op-names)) ; 213 -(defn generate-ndarray-api-file [] - (println "Generating ndarray-api file") - (write-to-file all-ndarray-api-functions - ndarray-api-gen-ns - "src/org/apache/clojure_mxnet/gen/ndarray_api.clj")) +(defn all-ndarray-random-api-functions [op-names] + (->> op-names + (filter #(= :random (op-name->namespace %))) + (mapv gen-ndarray-random-api-function))) -;;;;;;; NDArrayRandomAPI +(count (all-ndarray-random-api-functions op-names)) ;8 -(defn gen-ndarray-random-api-function [op-name] - (let [{:keys [fn-name fn-description args]} (gen-op-info op-name) - fn-name (remove-prefix "-random-" fn-name) - params (mapv (fn [{:keys [name] :as opts}] - (assoc opts :sym (symbol name))) - (conj args {:name "out" - :type "NDArray-or-Symbol" - :optional? true - :description "Output array."})) - doc (gen-ndarray-api-doc fn-description params) - opt-params (filter :optional? params) - req-params (remove :optional? params) - req-call (gen-ndarray-api-required-arity fn-name req-params) - default-call (gen-ndarray-api-default-arity op-name params)] - (if (= 1 (count req-params)) - `(~'defn ~(symbol fn-name) - ~doc - ~@default-call) - `(~'defn ~(symbol fn-name) - ~doc - ~req-call - ~default-call)))) - -(def all-ndarray-random-api-functions +(defn all-ndarray-sample-api-functions [op-names] (->> op-names - (filter #(clojure.string/includes? % "random_")) - (mapv gen-ndarray-random-api-function))) + (filter #(= :sample (op-name->namespace %))) + (mapv gen-ndarray-sample-api-function))) -(def ndarray-random-api-gen-ns "(ns - ^{:doc \"Experimental\"} - org.apache.clojure-mxnet.ndarray-random-api - (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max - min repeat reverse set sort take to-array empty shuffle - ref]) - (:require [org.apache.clojure-mxnet.shape :as mx-shape] - [org.apache.clojure-mxnet.util :as util]) - (:import (org.apache.mxnet NDArrayAPI)))") +(count (all-ndarray-sample-api-functions op-names)) ;8 + +(defn namespace-type->namespace-str [namespace-type] + (case namespace-type + :random "ndarray-random-api" + :sample "ndarray-sample-api" + :core "ndarray-api")) -(defn generate-ndarray-random-api-file [] - (println "Generating ndarray-random-api file") - (write-to-file all-ndarray-random-api-functions - ndarray-random-api-gen-ns - "src/org/apache/clojure_mxnet/gen/ndarray_random_api.clj")) +(defn ndarray-api-gen-ns [namespace-type] + (str + "(ns\n" + " ^{:doc \"Experimental\"}\n" + " org.apache.clojure-mxnet." (namespace-type->namespace-str namespace-type) "\n" + " (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max\n" + " min repeat reverse set sort take to-array empty shuffle\n" + " ref])\n" + " (:require [org.apache.clojure-mxnet.shape :as mx-shape]\n" + " [org.apache.clojure-mxnet.util :as util])\n" + " (:import (org.apache.mxnet NDArrayAPI)))")) + +(defn generate-ndarray-api-file [op-names] + (let [namespace-type :core] + (println "Generating" (namespace-type->namespace-str namespace-type) "file") + (write-to-file (all-ndarray-api-functions op-names) + (ndarray-api-gen-ns namespace-type) + "src/org/apache/clojure_mxnet/gen/ndarray_api.clj"))) + +(defn generate-ndarray-random-api-file [op-names] + (let [namespace-type :random] + (println "Generating" (namespace-type->namespace-str namespace-type) "file") + (write-to-file (all-ndarray-random-api-functions op-names) + (ndarray-api-gen-ns namespace-type) + "src/org/apache/clojure_mxnet/gen/ndarray_random_api.clj"))) + +(defn generate-ndarray-sample-api-file [op-names] + (let [namespace-type :sample] + (println "Generating" (namespace-type->namespace-str namespace-type) "file") + (write-to-file (all-ndarray-sample-api-functions op-names) + (ndarray-api-gen-ns namespace-type) + "src/org/apache/clojure_mxnet/gen/ndarray_sample_api.clj"))) ;;; autogen the files (do (generate-ndarray-file) - (generate-ndarray-api-file) - (generate-ndarray-random-api-file) + + ;; NDArrayAPI + (generate-ndarray-api-file op-names) + (generate-ndarray-random-api-file op-names) + (generate-ndarray-sample-api-file op-names) + (generate-symbol-file) - (generate-symbol-api-file) - (generate-symbol-random-api-file)) + + ;; SymbolAPI + (generate-symbol-api-file op-names) + (generate-symbol-random-api-file op-names)) (comment @@ -675,6 +714,8 @@ (gen-ndarray-random-api-function "random_randint") + (gen-ndarray-random-api-function "sample_normal") + (gen-symbol-random-api-function "random_poisson") ;; This generates a file with the bulk of the nd-array functions diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_sample_api.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_sample_api.clj new file mode 100644 index 000000000000..6e03d63fe7df --- /dev/null +++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_sample_api.clj @@ -0,0 +1,12 @@ +(ns org.apache.clojure-mxnet.ndarray-sample-api + "Experimental NDArray Random API" + (:require [org.apache.clojure-mxnet.base :as base] + [org.apache.clojure-mxnet.context :as mx-context] + [org.apache.clojure-mxnet.shape :as mx-shape] + [org.apache.clojure-mxnet.util :as util] + [clojure.reflect :as r] + [t6.from-scala.core :refer [$] :as $]) + (:import (org.apache.mxnet NDArrayAPI))) + +;; loads the generated functions into the namespace +(do (clojure.core/load "gen/ndarray_sample_api")) diff --git a/contrib/clojure-package/test/dev/generator_test.clj b/contrib/clojure-package/test/dev/generator_test.clj index cf28241c59e8..d1f17eb5606a 100644 --- a/contrib/clojure-package/test/dev/generator_test.clj +++ b/contrib/clojure-package/test/dev/generator_test.clj @@ -27,6 +27,18 @@ (is (= "foo-bar" (gen/clojure-case "Foo_Bar"))) (is (= "div+" (gen/clojure-case "/+")))) +(deftest random-fn-name->fn-name + (is (= "poisson" (gen/random-fn-name->fn-name "-random-poisson"))) + (is (= "sample-poisson" (gen/random-fn-name->fn-name "-sample-poisson")))) + +;; TODO +(deftest remove-prefix + (is (= 42 1))) + +;; TODO +(deftest ndarray-api-random? + (is (= 42 1))) + (defn ndarray-reflect-info [name] (->> gen/ndarray-public-no-default (filter #(= name (str (:name %)))) @@ -317,9 +329,9 @@ (deftest test-write-to-file (testing "symbol-api" (let [fname "test/test-symbol-api.clj" - _ (gen/write-to-file [(first gen/all-symbol-api-functions) - (second gen/all-symbol-api-functions)] - gen/symbol-api-gen-ns + fns (gen/all-symbol-api-functions gen/op-names) + _ (gen/write-to-file [(first fns) (second fns)] + (gen/symbol-api-gen-ns false) fname) good-contents (slurp "test/good-test-symbol-api.clj") contents (slurp fname)] @@ -336,9 +348,9 @@ (testing "ndarray-api" (let [fname "test/test-ndarray-api.clj" - _ (gen/write-to-file [(first gen/all-ndarray-api-functions) - (second gen/all-ndarray-api-functions)] - gen/ndarray-api-gen-ns + fns (gen/all-ndarray-api-functions gen/op-names) + _ (gen/write-to-file [(first fns) (second fns)] + (gen/ndarray-api-gen-ns false) fname) good-contents (slurp "test/good-test-ndarray-api.clj") contents (slurp fname)] From 3ce373040c1cf5b838da9d7590f826c77fd8904f Mon Sep 17 00:00:00 2001 From: Chouffe Date: Wed, 24 Apr 2019 17:13:33 +0200 Subject: [PATCH 3/4] add unit tests, add licence --- contrib/clojure-package/src/dev/generator.clj | 224 +++++++----------- .../org/apache/clojure_mxnet/ndarray_api.clj | 8 +- .../clojure_mxnet/ndarray_random_api.clj | 16 ++ .../clojure_mxnet/ndarray_sample_api.clj | 12 - .../clojure_mxnet/symbol_random_api.clj | 32 +++ .../test/dev/generator_test.clj | 39 ++- .../test/good-test-ndarray-random-api.clj | 62 +++++ .../test/good-test-symbol-random-api.clj | 83 +++++++ .../test/test-ndarray-random-api.clj | 62 +++++ .../test/test-symbol-random-api.clj | 83 +++++++ 10 files changed, 462 insertions(+), 159 deletions(-) delete mode 100644 contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_sample_api.clj create mode 100644 contrib/clojure-package/src/org/apache/clojure_mxnet/symbol_random_api.clj create mode 100644 contrib/clojure-package/test/good-test-ndarray-random-api.clj create mode 100644 contrib/clojure-package/test/good-test-symbol-random-api.clj create mode 100644 contrib/clojure-package/test/test-ndarray-random-api.clj create mode 100644 contrib/clojure-package/test/test-symbol-random-api.clj diff --git a/contrib/clojure-package/src/dev/generator.clj b/contrib/clojure-package/src/dev/generator.clj index 339943500ea7..9a3e218d5dc0 100644 --- a/contrib/clojure-package/src/dev/generator.clj +++ b/contrib/clojure-package/src/dev/generator.clj @@ -132,33 +132,27 @@ replacement "$1"] (clojure.string/replace s regex replacement))) -(defn op-name->namespace - [op-name] - (cond - (#{"uniform" "normal"} op-name) :deprecated - (clojure.string/starts-with? op-name "random_") :random - (clojure.string/starts-with? op-name "sample_") :sample - :else :core)) - -(op-name->namespace "random_uniform") -(op-name->namespace "sample_uniform") -(op-name->namespace "activation") - (defn in-namespace-random? [op-name] - (or (#{"uniform" "normal"} op-name) - (clojure.string/includes? op-name "random_") + (or (clojure.string/includes? op-name "random_") (clojure.string/includes? op-name "sample_"))) +(defn op-name->namespace-type [op-name] + (cond + (#{"uniform" "normal"} op-name) :deprecated + (clojure.string/includes? op-name "random_") :random + (clojure.string/includes? op-name "sample_") :random + :else :core)) + ;;;;;;; Common operations (def libinfo (Base/_LIB)) (def op-names (let [l ($ ListBuffer/empty)] - (do (.mxListAllOpNames libinfo l) - (remove #(or (= "Custom" %) - (re-matches #"^_.*" %)) - (util/buffer->vec l))))) + (.mxListAllOpNames libinfo l) + (->> l + (util/buffer->vec) + (remove #(or (= "Custom" %) (re-matches #"^_.*" %)))))) (defn- parse-arg-type [s] (let [[_ var-arg-type _ set-arg-type arg-spec _ type-req _ default-val] (re-find #"(([\w-\[\]\s]+)|\{([^}]+)\})\s*(\([^)]+\))?(,\s*(optional|required)(,\s*default=(.*))?)?" s)] @@ -412,6 +406,17 @@ ;;;;;;; SymbolAPI +(defn fn-name->random-fn-name + [fn-name] + (cond + (clojure.string/starts-with? fn-name "-random-") + (remove-prefix "-random-" fn-name) + + (clojure.string/starts-with? fn-name "-sample-") + (str (remove-prefix "-sample-" fn-name) "-like") + + :else fn-name)) + (defn symbol-api-coerce-param [{:keys [name sym type optional?]}] (let [coerced-param (case type @@ -452,91 +457,78 @@ (~(symbol (str "SymbolAPI/" op-name)) ~@coerced-params))))) -(defn gen-symbol-api-function [op-name] - (let [{:keys [fn-name fn-description args]} (gen-op-info op-name) - params (mapv (fn [{:keys [name type optional?] :as opts}] - (assoc opts - :sym (symbol name) - :optional? (or optional? - (= "NDArray-or-Symbol" type)))) - (conj args - {:name "name" - :type "String" - :optional? true - :description "Name of the symbol"} - {:name "attr" - :type "Map[String, String]" - :optional? true - :description "Attributes of the symbol"})) - doc (gen-symbol-api-doc fn-description params) - default-call (gen-symbol-api-default-arity op-name params)] - `(~'defn ~(symbol fn-name) - ~doc - ~@default-call))) - -(defn all-symbol-api-functions [op-names] - (->> op-names - (remove in-namespace-random?) - (mapv gen-symbol-api-function))) - (defn symbol-api-gen-ns - [random?] + [random-namespace?] (str "(ns\n" " ^{:doc \"Experimental\"}\n" - " org.apache.clojure-mxnet.symbol-api\n" + (if random-namespace? + " org.apache.clojure-mxnet.symbol-random-api\n" + " org.apache.clojure-mxnet.symbol-api\n") " (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max\n" " min repeat reverse set sort take to-array empty sin\n" " get apply shuffle ref])\n" " (:require [org.apache.clojure-mxnet.util :as util]\n" " [org.apache.clojure-mxnet.shape :as mx-shape])\n" - (if random? - " (:import (org.apache.mxnet SymbolAPI SymbolRandomAPI)))" - " (:import (org.apache.mxnet SymbolAPI)))"))) + " (:import (org.apache.mxnet SymbolAPI)))")) -(defn generate-symbol-api-file [op-names] - (println "Generating symbol-api file") - (write-to-file (all-symbol-api-functions op-names) - (symbol-api-gen-ns false) - "src/org/apache/clojure_mxnet/gen/symbol_api.clj")) +(defn make-gen-symbol-api-function + [{:keys [fn-name->fn-name] :or {fn-name->fn-name identity}}] + (fn [op-name] + (let [{:keys [fn-name fn-description args]} + (-> op-name (gen-op-info) (update :fn-name fn-name->fn-name)) + params (mapv (fn [{:keys [name type optional?] :as opts}] + (assoc opts + :sym (symbol name) + :optional? (or optional? + (= "NDArray-or-Symbol" type)))) + (conj args + {:name "name" + :type "String" + :optional? true + :description "Name of the symbol"} + {:name "attr" + :type "Map[String, String]" + :optional? true + :description "Attributes of the symbol"})) + doc (gen-symbol-api-doc fn-description params) + default-call (gen-symbol-api-default-arity op-name params)] + `(~'defn ~(symbol fn-name) + ~doc + ~@default-call)))) + +(def gen-symbol-api-function + (make-gen-symbol-api-function {})) + +(def gen-symbol-random-api-function + (make-gen-symbol-api-function {:fn-name->fn-name fn-name->random-fn-name})) -;;;;;;; SymbolRandomAPI - -(defn gen-symbol-random-api-function [op-name] - (let [{:keys [fn-name fn-description args]} (gen-op-info op-name) - fn-name (remove-prefix "-random-" fn-name) - params (mapv (fn [{:keys [name type optional?] :as opts}] - (assoc opts - :sym (symbol name) - :optional? (or optional? - (= "NDArray-or-Symbol" type)))) - (conj args - {:name "name" - :type "String" - :optional? true - :description "Name of the symbol"} - {:name "attr" - :type "Map[String, String]" - :optional? true - :description "Attributes of the symbol"})) - doc (gen-symbol-api-doc fn-description params) - default-call (gen-symbol-api-default-arity op-name params)] - `(~'defn ~(symbol fn-name) - ~doc - ~@default-call))) +(defn all-symbol-api-functions [op-names] + (->> op-names + (filter #(= :core (op-name->namespace-type %))) + (mapv gen-symbol-api-function))) + +(count (all-symbol-api-functions op-names)) ;215 (defn all-symbol-random-api-functions [op-names] (->> op-names - (filter in-namespace-random?) + (filter #(= :random (op-name->namespace-type %))) (mapv gen-symbol-random-api-function))) +(count (all-symbol-random-api-functions op-names)) ;16 + +(defn generate-symbol-api-file [op-names] + (println "Generating symbol-api file") + (write-to-file (all-symbol-api-functions op-names) + (symbol-api-gen-ns false) + "src/org/apache/clojure_mxnet/gen/symbol_api.clj")) + (defn generate-symbol-random-api-file [op-names] (println "Generating symbol-random-api file") (write-to-file (all-symbol-random-api-functions op-names) (symbol-api-gen-ns true) "src/org/apache/clojure_mxnet/gen/symbol_random_api.clj")) - ;;;;;;; NDArrayAPI (defn ndarray-api-coerce-param @@ -604,62 +596,33 @@ ~req-call ~default-call))))) -(defn fn-name->random-fn-name - [fn-name] - (if (clojure.string/starts-with? fn-name "-random-") - (remove-prefix "-random-" fn-name) - fn-name)) - -(defn fn-name->sample-fn-name - [fn-name] - (if (clojure.string/starts-with? fn-name "-sample-") - (remove-prefix "-sample-" fn-name) - fn-name)) - -(fn-name->random-fn-name "-random-exponential") ;"exponential" -(fn-name->sample-fn-name "-sample-poisson") ;"poisson" - (def gen-ndarray-api-function (make-gen-ndarray-api-function {})) (def gen-ndarray-random-api-function (make-gen-ndarray-api-function {:fn-name->fn-name fn-name->random-fn-name})) -(def gen-ndarray-sample-api-function - (make-gen-ndarray-api-function {:fn-name->fn-name fn-name->sample-fn-name})) - (defn all-ndarray-api-functions [op-names] (->> op-names - (filter #(= :core (op-name->namespace %))) + (filter #(= :core (op-name->namespace-type %))) (mapv gen-ndarray-api-function))) (count (all-ndarray-api-functions op-names)) ; 213 (defn all-ndarray-random-api-functions [op-names] (->> op-names - (filter #(= :random (op-name->namespace %))) + (filter #(= :random (op-name->namespace-type %))) (mapv gen-ndarray-random-api-function))) -(count (all-ndarray-random-api-functions op-names)) ;8 - -(defn all-ndarray-sample-api-functions [op-names] - (->> op-names - (filter #(= :sample (op-name->namespace %))) - (mapv gen-ndarray-sample-api-function))) - -(count (all-ndarray-sample-api-functions op-names)) ;8 - -(defn namespace-type->namespace-str [namespace-type] - (case namespace-type - :random "ndarray-random-api" - :sample "ndarray-sample-api" - :core "ndarray-api")) +(count (all-ndarray-random-api-functions op-names)) ;16 -(defn ndarray-api-gen-ns [namespace-type] +(defn ndarray-api-gen-ns [random-namespace?] (str "(ns\n" " ^{:doc \"Experimental\"}\n" - " org.apache.clojure-mxnet." (namespace-type->namespace-str namespace-type) "\n" + (if random-namespace? + " org.apache.clojure-mxnet.ndarray-random-api\n" + " org.apache.clojure-mxnet.ndarray-api\n") " (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max\n" " min repeat reverse set sort take to-array empty shuffle\n" " ref])\n" @@ -668,25 +631,17 @@ " (:import (org.apache.mxnet NDArrayAPI)))")) (defn generate-ndarray-api-file [op-names] - (let [namespace-type :core] - (println "Generating" (namespace-type->namespace-str namespace-type) "file") - (write-to-file (all-ndarray-api-functions op-names) - (ndarray-api-gen-ns namespace-type) - "src/org/apache/clojure_mxnet/gen/ndarray_api.clj"))) + (println "Generating ndarray-api file") + (write-to-file (all-ndarray-api-functions op-names) + (ndarray-api-gen-ns false) + "src/org/apache/clojure_mxnet/gen/ndarray_api.clj")) (defn generate-ndarray-random-api-file [op-names] - (let [namespace-type :random] - (println "Generating" (namespace-type->namespace-str namespace-type) "file") - (write-to-file (all-ndarray-random-api-functions op-names) - (ndarray-api-gen-ns namespace-type) - "src/org/apache/clojure_mxnet/gen/ndarray_random_api.clj"))) - -(defn generate-ndarray-sample-api-file [op-names] - (let [namespace-type :sample] - (println "Generating" (namespace-type->namespace-str namespace-type) "file") - (write-to-file (all-ndarray-sample-api-functions op-names) - (ndarray-api-gen-ns namespace-type) - "src/org/apache/clojure_mxnet/gen/ndarray_sample_api.clj"))) + (println "Generating ndarray-random-api file") + (write-to-file (all-ndarray-random-api-functions op-names) + (ndarray-api-gen-ns true) + "src/org/apache/clojure_mxnet/gen/ndarray_random_api.clj")) + ;;; autogen the files (do @@ -695,7 +650,6 @@ ;; NDArrayAPI (generate-ndarray-api-file op-names) (generate-ndarray-random-api-file op-names) - (generate-ndarray-sample-api-file op-names) (generate-symbol-file) diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_api.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_api.clj index 70359a6ef9b7..e222775c60f6 100644 --- a/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_api.clj +++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_api.clj @@ -16,10 +16,10 @@ (ns org.apache.clojure-mxnet.ndarray-api "Experimental NDArray API" - (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max - min repeat reverse set sort take to-array empty shuffle - ref]) - + (:refer-clojure + :exclude [* - + > >= < <= / cast concat flatten identity load max + min repeat reverse set sort take to-array empty shuffle + ref]) (:require [org.apache.clojure-mxnet.base :as base] [org.apache.clojure-mxnet.context :as mx-context] [org.apache.clojure-mxnet.shape :as mx-shape] diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_random_api.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_random_api.clj index 6027bde7ed7a..1f45b6d4d646 100644 --- a/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_random_api.clj +++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_random_api.clj @@ -1,3 +1,19 @@ +;; 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.ndarray-random-api "Experimental NDArray Random API" (:require [org.apache.clojure-mxnet.base :as base] diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_sample_api.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_sample_api.clj deleted file mode 100644 index 6e03d63fe7df..000000000000 --- a/contrib/clojure-package/src/org/apache/clojure_mxnet/ndarray_sample_api.clj +++ /dev/null @@ -1,12 +0,0 @@ -(ns org.apache.clojure-mxnet.ndarray-sample-api - "Experimental NDArray Random API" - (:require [org.apache.clojure-mxnet.base :as base] - [org.apache.clojure-mxnet.context :as mx-context] - [org.apache.clojure-mxnet.shape :as mx-shape] - [org.apache.clojure-mxnet.util :as util] - [clojure.reflect :as r] - [t6.from-scala.core :refer [$] :as $]) - (:import (org.apache.mxnet NDArrayAPI))) - -;; loads the generated functions into the namespace -(do (clojure.core/load "gen/ndarray_sample_api")) diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/symbol_random_api.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/symbol_random_api.clj new file mode 100644 index 000000000000..76f6fdefc334 --- /dev/null +++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/symbol_random_api.clj @@ -0,0 +1,32 @@ +;; 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.symbol-random-api + "Experimental Symbol Random API" + (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max + min repeat reverse set sort take to-array empty sin + get apply shuffle ref]) + (:require [org.apache.clojure-mxnet.base :as base] + [org.apache.clojure-mxnet.context :as mx-context] + [org.apache.clojure-mxnet.executor :as ex] + [org.apache.clojure-mxnet.shape :as mx-shape] + [org.apache.clojure-mxnet.util :as util] + [t6.from-scala.core :refer [$] :as $] + [org.apache.clojure-mxnet.ndarray :as ndarray]) + (:import (org.apache.mxnet SymbolAPI))) + +;; loads the generated functions into the namespace +(do (clojure.core/load "gen/symbol_random_api")) diff --git a/contrib/clojure-package/test/dev/generator_test.clj b/contrib/clojure-package/test/dev/generator_test.clj index d1f17eb5606a..acc81afcbcd5 100644 --- a/contrib/clojure-package/test/dev/generator_test.clj +++ b/contrib/clojure-package/test/dev/generator_test.clj @@ -27,17 +27,19 @@ (is (= "foo-bar" (gen/clojure-case "Foo_Bar"))) (is (= "div+" (gen/clojure-case "/+")))) -(deftest random-fn-name->fn-name - (is (= "poisson" (gen/random-fn-name->fn-name "-random-poisson"))) - (is (= "sample-poisson" (gen/random-fn-name->fn-name "-sample-poisson")))) +(deftest fn-name->random-fn-name + (is (= "poisson" (gen/fn-name->random-fn-name "-random-poisson"))) + (is (= "poisson-like" (gen/fn-name->random-fn-name "-sample-poisson")))) -;; TODO (deftest remove-prefix - (is (= 42 1))) + (is (= "randint" (gen/remove-prefix "-random-" "-random-randint"))) + (is (= "exponential" (gen/remove-prefix "-sample-" "-sample-exponential")))) -;; TODO -(deftest ndarray-api-random? - (is (= 42 1))) +(deftest in-namespace-random? + (is (gen/in-namespace-random? "random_randint")) + (is (gen/in-namespace-random? "sample_poisson")) + (is (not (gen/in-namespace-random? "rnn"))) + (is (not (gen/in-namespace-random? "activation")))) (defn ndarray-reflect-info [name] (->> gen/ndarray-public-no-default @@ -337,6 +339,17 @@ contents (slurp fname)] (is (= good-contents contents)))) + (testing "symbol-random-api" + (let [fname "test/test-symbol-random-api.clj" + fns (gen/all-symbol-random-api-functions gen/op-names) + _ (gen/write-to-file [(first fns) (second fns)] + (gen/symbol-api-gen-ns true) + fname) + good-contents (slurp "test/good-test-symbol-random-api.clj") + contents (slurp fname)] + (is (= good-contents contents)))) + + (testing "symbol" (let [fname "test/test-symbol.clj" _ (gen/write-to-file [(first gen/all-symbol-functions)] @@ -356,6 +369,16 @@ contents (slurp fname)] (is (= good-contents contents)))) + (testing "ndarray-random-api" + (let [fname "test/test-ndarray-random-api.clj" + fns (gen/all-ndarray-random-api-functions gen/op-names) + _ (gen/write-to-file [(first fns) (second fns)] + (gen/ndarray-api-gen-ns true) + fname) + good-contents (slurp "test/good-test-ndarray-random-api.clj") + contents (slurp fname)] + (is (= good-contents contents)))) + (testing "ndarray" (let [fname "test/test-ndarray.clj" _ (gen/write-to-file [(first gen/all-ndarray-functions)] diff --git a/contrib/clojure-package/test/good-test-ndarray-random-api.clj b/contrib/clojure-package/test/good-test-ndarray-random-api.clj new file mode 100644 index 000000000000..0937b5a88be9 --- /dev/null +++ b/contrib/clojure-package/test/good-test-ndarray-random-api.clj @@ -0,0 +1,62 @@ +(ns + ^{:doc "Experimental"} + org.apache.clojure-mxnet.ndarray-random-api + (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max + min repeat reverse set sort take to-array empty shuffle + ref]) + (:require [org.apache.clojure-mxnet.shape :as mx-shape] + [org.apache.clojure-mxnet.util :as util]) + (:import (org.apache.mxnet NDArrayAPI))) + +;; Do not edit - this is auto-generated + +;; 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. +;; + + + + +(defn + exponential + "Draw random samples from an exponential distribution.\n\nSamples are distributed according to an exponential distribution parametrized by *lambda* (rate).\n\nExample::\n\n exponential(lam=4, shape=(2,2)) = [[ 0.0097189 , 0.08999364],\n [ 0.04146638, 0.31715935]]\n\n\nDefined in src/operator/random/sample_op.cc:L137\n\n`lam`: Lambda parameter (rate) of the exponential distribution. (optional)\n`shape`: Shape of the output. (optional)\n`ctx`: Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls. (optional)\n`dtype`: DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None). (optional)\n`out`: Output array. (optional)\n" + ([] (exponential {})) + ([{:keys [lam shape ctx dtype out], + :or {lam nil, shape nil, ctx nil, dtype nil, out nil}, + :as opts}] + (util/coerce-return + (NDArrayAPI/random_exponential + (util/->option lam) + (util/->option (clojure.core/when shape (mx-shape/->shape shape))) + (util/->option ctx) + (util/->option dtype) + (util/->option out))))) + +(defn + gamma + "Draw random samples from a gamma distribution.\n\nSamples are distributed according to a gamma distribution parametrized by *alpha* (shape) and *beta* (scale).\n\nExample::\n\n gamma(alpha=9, beta=0.5, shape=(2,2)) = [[ 7.10486984, 3.37695289],\n [ 3.91697288, 3.65933681]]\n\n\nDefined in src/operator/random/sample_op.cc:L125\n\n`alpha`: Alpha parameter (shape) of the gamma distribution. (optional)\n`beta`: Beta parameter (scale) of the gamma distribution. (optional)\n`shape`: Shape of the output. (optional)\n`ctx`: Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls. (optional)\n`dtype`: DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None). (optional)\n`out`: Output array. (optional)\n" + ([] (gamma {})) + ([{:keys [alpha beta shape ctx dtype out], + :or {alpha nil, beta nil, shape nil, ctx nil, dtype nil, out nil}, + :as opts}] + (util/coerce-return + (NDArrayAPI/random_gamma + (util/->option alpha) + (util/->option beta) + (util/->option (clojure.core/when shape (mx-shape/->shape shape))) + (util/->option ctx) + (util/->option dtype) + (util/->option out))))) + diff --git a/contrib/clojure-package/test/good-test-symbol-random-api.clj b/contrib/clojure-package/test/good-test-symbol-random-api.clj new file mode 100644 index 000000000000..100aa3432a52 --- /dev/null +++ b/contrib/clojure-package/test/good-test-symbol-random-api.clj @@ -0,0 +1,83 @@ +(ns + ^{:doc "Experimental"} + org.apache.clojure-mxnet.symbol-random-api + (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max + min repeat reverse set sort take to-array empty sin + get apply shuffle ref]) + (:require [org.apache.clojure-mxnet.util :as util] + [org.apache.clojure-mxnet.shape :as mx-shape]) + (:import (org.apache.mxnet SymbolAPI))) + +;; Do not edit - this is auto-generated + +;; 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. +;; + + + + +(defn + exponential + "Draw random samples from an exponential distribution.\n\nSamples are distributed according to an exponential distribution parametrized by *lambda* (rate).\n\nExample::\n\n exponential(lam=4, shape=(2,2)) = [[ 0.0097189 , 0.08999364],\n [ 0.04146638, 0.31715935]]\n\n\nDefined in src/operator/random/sample_op.cc:L137\n\n`lam`: Lambda parameter (rate) of the exponential distribution. (optional)\n`shape`: Shape of the output. (optional)\n`ctx`: Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls. (optional)\n`dtype`: DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None). (optional)\n`name`: Name of the symbol (optional)\n`attr`: Attributes of the symbol (optional)\n" + [{:keys [lam shape ctx dtype name attr], + :or {lam nil, shape nil, ctx nil, dtype nil, name nil, attr nil}, + :as opts}] + (util/coerce-return + (SymbolAPI/random_exponential + (util/->option lam) + (util/->option (clojure.core/when shape (mx-shape/->shape shape))) + (util/->option ctx) + (util/->option dtype) + name + (clojure.core/when + attr + (clojure.core/->> + attr + (clojure.core/mapv + (clojure.core/fn [[k v]] [k (clojure.core/str v)])) + (clojure.core/into {}) + util/convert-map))))) + +(defn + gamma + "Draw random samples from a gamma distribution.\n\nSamples are distributed according to a gamma distribution parametrized by *alpha* (shape) and *beta* (scale).\n\nExample::\n\n gamma(alpha=9, beta=0.5, shape=(2,2)) = [[ 7.10486984, 3.37695289],\n [ 3.91697288, 3.65933681]]\n\n\nDefined in src/operator/random/sample_op.cc:L125\n\n`alpha`: Alpha parameter (shape) of the gamma distribution. (optional)\n`beta`: Beta parameter (scale) of the gamma distribution. (optional)\n`shape`: Shape of the output. (optional)\n`ctx`: Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls. (optional)\n`dtype`: DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None). (optional)\n`name`: Name of the symbol (optional)\n`attr`: Attributes of the symbol (optional)\n" + [{:keys [alpha beta shape ctx dtype name attr], + :or + {alpha nil, + beta nil, + shape nil, + ctx nil, + dtype nil, + name nil, + attr nil}, + :as opts}] + (util/coerce-return + (SymbolAPI/random_gamma + (util/->option alpha) + (util/->option beta) + (util/->option (clojure.core/when shape (mx-shape/->shape shape))) + (util/->option ctx) + (util/->option dtype) + name + (clojure.core/when + attr + (clojure.core/->> + attr + (clojure.core/mapv + (clojure.core/fn [[k v]] [k (clojure.core/str v)])) + (clojure.core/into {}) + util/convert-map))))) + diff --git a/contrib/clojure-package/test/test-ndarray-random-api.clj b/contrib/clojure-package/test/test-ndarray-random-api.clj new file mode 100644 index 000000000000..0937b5a88be9 --- /dev/null +++ b/contrib/clojure-package/test/test-ndarray-random-api.clj @@ -0,0 +1,62 @@ +(ns + ^{:doc "Experimental"} + org.apache.clojure-mxnet.ndarray-random-api + (:refer-clojure :exclude [* - + > >= < <= / cast concat flatten identity load max + min repeat reverse set sort take to-array empty shuffle + ref]) + (:require [org.apache.clojure-mxnet.shape :as mx-shape] + [org.apache.clojure-mxnet.util :as util]) + (:import (org.apache.mxnet NDArrayAPI))) + +;; Do not edit - this is auto-generated + +;; 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. +;; + + + + +(defn + exponential + "Draw random samples from an exponential distribution.\n\nSamples are distributed according to an exponential distribution parametrized by *lambda* (rate).\n\nExample::\n\n exponential(lam=4, shape=(2,2)) = [[ 0.0097189 , 0.08999364],\n [ 0.04146638, 0.31715935]]\n\n\nDefined in src/operator/random/sample_op.cc:L137\n\n`lam`: Lambda parameter (rate) of the exponential distribution. (optional)\n`shape`: Shape of the output. (optional)\n`ctx`: Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls. (optional)\n`dtype`: DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None). (optional)\n`out`: Output array. (optional)\n" + ([] (exponential {})) + ([{:keys [lam shape ctx dtype out], + :or {lam nil, shape nil, ctx nil, dtype nil, out nil}, + :as opts}] + (util/coerce-return + (NDArrayAPI/random_exponential + (util/->option lam) + (util/->option (clojure.core/when shape (mx-shape/->shape shape))) + (util/->option ctx) + (util/->option dtype) + (util/->option out))))) + +(defn + gamma + "Draw random samples from a gamma distribution.\n\nSamples are distributed according to a gamma distribution parametrized by *alpha* (shape) and *beta* (scale).\n\nExample::\n\n gamma(alpha=9, beta=0.5, shape=(2,2)) = [[ 7.10486984, 3.37695289],\n [ 3.91697288, 3.65933681]]\n\n\nDefined in src/operator/random/sample_op.cc:L125\n\n`alpha`: Alpha parameter (shape) of the gamma distribution. (optional)\n`beta`: Beta parameter (scale) of the gamma distribution. (optional)\n`shape`: Shape of the output. (optional)\n`ctx`: Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls. (optional)\n`dtype`: DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None). (optional)\n`out`: Output array. (optional)\n" + ([] (gamma {})) + ([{:keys [alpha beta shape ctx dtype out], + :or {alpha nil, beta nil, shape nil, ctx nil, dtype nil, out nil}, + :as opts}] + (util/coerce-return + (NDArrayAPI/random_gamma + (util/->option alpha) + (util/->option beta) + (util/->option (clojure.core/when shape (mx-shape/->shape shape))) + (util/->option ctx) + (util/->option dtype) + (util/->option out))))) + diff --git a/contrib/clojure-package/test/test-symbol-random-api.clj b/contrib/clojure-package/test/test-symbol-random-api.clj new file mode 100644 index 000000000000..100aa3432a52 --- /dev/null +++ b/contrib/clojure-package/test/test-symbol-random-api.clj @@ -0,0 +1,83 @@ +(ns + ^{:doc "Experimental"} + org.apache.clojure-mxnet.symbol-random-api + (:refer-clojure :exclude [* - + > >= < <= / cast concat identity flatten load max + min repeat reverse set sort take to-array empty sin + get apply shuffle ref]) + (:require [org.apache.clojure-mxnet.util :as util] + [org.apache.clojure-mxnet.shape :as mx-shape]) + (:import (org.apache.mxnet SymbolAPI))) + +;; Do not edit - this is auto-generated + +;; 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. +;; + + + + +(defn + exponential + "Draw random samples from an exponential distribution.\n\nSamples are distributed according to an exponential distribution parametrized by *lambda* (rate).\n\nExample::\n\n exponential(lam=4, shape=(2,2)) = [[ 0.0097189 , 0.08999364],\n [ 0.04146638, 0.31715935]]\n\n\nDefined in src/operator/random/sample_op.cc:L137\n\n`lam`: Lambda parameter (rate) of the exponential distribution. (optional)\n`shape`: Shape of the output. (optional)\n`ctx`: Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls. (optional)\n`dtype`: DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None). (optional)\n`name`: Name of the symbol (optional)\n`attr`: Attributes of the symbol (optional)\n" + [{:keys [lam shape ctx dtype name attr], + :or {lam nil, shape nil, ctx nil, dtype nil, name nil, attr nil}, + :as opts}] + (util/coerce-return + (SymbolAPI/random_exponential + (util/->option lam) + (util/->option (clojure.core/when shape (mx-shape/->shape shape))) + (util/->option ctx) + (util/->option dtype) + name + (clojure.core/when + attr + (clojure.core/->> + attr + (clojure.core/mapv + (clojure.core/fn [[k v]] [k (clojure.core/str v)])) + (clojure.core/into {}) + util/convert-map))))) + +(defn + gamma + "Draw random samples from a gamma distribution.\n\nSamples are distributed according to a gamma distribution parametrized by *alpha* (shape) and *beta* (scale).\n\nExample::\n\n gamma(alpha=9, beta=0.5, shape=(2,2)) = [[ 7.10486984, 3.37695289],\n [ 3.91697288, 3.65933681]]\n\n\nDefined in src/operator/random/sample_op.cc:L125\n\n`alpha`: Alpha parameter (shape) of the gamma distribution. (optional)\n`beta`: Beta parameter (scale) of the gamma distribution. (optional)\n`shape`: Shape of the output. (optional)\n`ctx`: Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls. (optional)\n`dtype`: DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None). (optional)\n`name`: Name of the symbol (optional)\n`attr`: Attributes of the symbol (optional)\n" + [{:keys [alpha beta shape ctx dtype name attr], + :or + {alpha nil, + beta nil, + shape nil, + ctx nil, + dtype nil, + name nil, + attr nil}, + :as opts}] + (util/coerce-return + (SymbolAPI/random_gamma + (util/->option alpha) + (util/->option beta) + (util/->option (clojure.core/when shape (mx-shape/->shape shape))) + (util/->option ctx) + (util/->option dtype) + name + (clojure.core/when + attr + (clojure.core/->> + attr + (clojure.core/mapv + (clojure.core/fn [[k v]] [k (clojure.core/str v)])) + (clojure.core/into {}) + util/convert-map))))) + From 0b6466a9dc6255316046c1b67ba26d8d2f541378 Mon Sep 17 00:00:00 2001 From: Chouffe Date: Wed, 24 Apr 2019 17:15:01 +0200 Subject: [PATCH 4/4] fix typo: scale to scala --- contrib/clojure-package/src/dev/generator.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/clojure-package/src/dev/generator.clj b/contrib/clojure-package/src/dev/generator.clj index 9a3e218d5dc0..71273af0e456 100644 --- a/contrib/clojure-package/src/dev/generator.clj +++ b/contrib/clojure-package/src/dev/generator.clj @@ -29,7 +29,7 @@ (defn clojure-case - "Transforms a scale string (function name) to clojure case" + "Transforms a scala string (function name) to clojure case" [string] (-> string (clojure.string/replace #"(\s+)([A-Z][a-z]+)" "$1-$2")