Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Fix ordering of keys using array-map
Browse files Browse the repository at this point in the history
  • Loading branch information
kedarbellare committed Apr 7, 2019
1 parent 7e6e207 commit ffb73f8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 72 deletions.
24 changes: 14 additions & 10 deletions contrib/clojure-package/src/dev/generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,11 @@
:description "Symbol attributes"}))
opt-params (filter :optional? params)
coerced-params (mapv symbol-api-coerce-param params)
default-call `([{:keys ~(mapv :sym params)
:or ~(into {} (mapv #(vector (:sym %) nil)
opt-params))
:as ~'opts}]
default-args (array-map :keys (mapv :sym params)
:or (into {} (mapv #(vector (:sym %) nil)
opt-params))
:as 'opts)
default-call `([~default-args]
(~'util/coerce-return
(~(symbol (str "SymbolAPI/" op-name))
~@coerced-params)))]
Expand Down Expand Up @@ -469,14 +470,15 @@
req-params (filter #(not (:optional? %)) params)
opt-params (filter :optional? params)
coerced-params (mapv ndarray-api-coerce-param params)
req-args (into {} (mapv #(vector (keyword (:sym %)) (:sym %))
req-params))
req-call `(~(mapv :sym req-params)
(~(symbol fn-name)
~(into {} (mapv #(vector (keyword (:sym %)) (:sym %))
req-params))))
default-call `([{:keys ~(mapv :sym params)
:or ~(into {} (mapv #(vector (:sym %) nil)
(~(symbol fn-name) ~req-args))
default-args (array-map :keys (mapv :sym params)
:or (into {} (mapv #(vector (:sym %) nil)
opt-params))
:as ~'opts}]
:as 'opts)
default-call `([~default-args]
(~'util/coerce-return
(~(symbol (str "NDArrayAPI/" op-name))
~@coerced-params)))]
Expand Down Expand Up @@ -517,6 +519,8 @@

(comment

(gen-ndarray-api-function "Activation")

;; This generates a file with the bulk of the nd-array functions
(generate-ndarray-file)

Expand Down
26 changes: 13 additions & 13 deletions contrib/clojure-package/test/good-test-ndarray-api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
activation
"Applies an activation function element-wise to the input.\n\nThe following activation functions are supported:\n\n- `relu`: Rectified Linear Unit, :math:`y = max(x, 0)`\n- `sigmoid`: :math:`y = \\frac{1}{1 + exp(-x)}`\n- `tanh`: Hyperbolic tangent, :math:`y = \\frac{exp(x) - exp(-x)}{exp(x) + exp(-x)}`\n- `softrelu`: Soft ReLU, or SoftPlus, :math:`y = log(1 + exp(x))`\n- `softsign`: :math:`y = \\frac{x}{1 + abs(x)}`\n\n\n\nDefined in src/operator/nn/activation.cc:L167"
([data act-type] (activation {:data data, :act-type act-type}))
([{:as opts, :or {out nil}, :keys [data act-type out]}]
([{:keys [data act-type out], :or {out nil}, :as opts}]
(util/coerce-return
(NDArrayAPI/Activation data act-type (util/->option out)))))

Expand All @@ -45,17 +45,7 @@
:beta beta,
:moving-mean moving-mean,
:moving-var moving-var}))
([{:as opts,
:or
{eps nil,
momentum nil,
fix-gamma nil,
use-global-stats nil,
output-mean-var nil,
axis nil,
cudnn-off nil,
out nil},
:keys
([{:keys
[data
gamma
beta
Expand All @@ -68,7 +58,17 @@
output-mean-var
axis
cudnn-off
out]}]
out],
:or
{eps nil,
momentum nil,
fix-gamma nil,
use-global-stats nil,
output-mean-var nil,
axis nil,
cudnn-off nil,
out nil},
:as opts}]
(util/coerce-return
(NDArrayAPI/BatchNorm
data
Expand Down
36 changes: 18 additions & 18 deletions contrib/clojure-package/test/good-test-symbol-api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,30 @@
(defn
activation
"Applies an activation function element-wise to the input.\n\nThe following activation functions are supported:\n\n- `relu`: Rectified Linear Unit, :math:`y = max(x, 0)`\n- `sigmoid`: :math:`y = \\frac{1}{1 + exp(-x)}`\n- `tanh`: Hyperbolic tangent, :math:`y = \\frac{exp(x) - exp(-x)}{exp(x) + exp(-x)}`\n- `softrelu`: Soft ReLU, or SoftPlus, :math:`y = log(1 + exp(x))`\n- `softsign`: :math:`y = \\frac{x}{1 + abs(x)}`\n\n\n\nDefined in src/operator/nn/activation.cc:L167"
[{:as opts,
[{:keys [data act-type name attr],
:or {data nil, name nil, attr nil},
:keys [data act-type name attr]}]
:as opts}]
(util/coerce-return
(SymbolAPI/Activation (util/->option data) act-type name attr)))

(defn
batch-norm
"Batch normalization.\n\nNormalizes a data batch by mean and variance, and applies a scale ``gamma`` as\nwell as offset ``beta``.\n\nAssume the input has more than one dimension and we normalize along axis 1.\nWe first compute the mean and variance along this axis:\n\n.. math::\n\n data\\_mean[i] = mean(data[:,i,:,...]) \\\\\n data\\_var[i] = var(data[:,i,:,...])\n\nThen compute the normalized output, which has the same shape as input, as following:\n\n.. math::\n\n out[:,i,:,...] = \\frac{data[:,i,:,...] - data\\_mean[i]}{\\sqrt{data\\_var[i]+\\epsilon}} * gamma[i] + beta[i]\n\nBoth *mean* and *var* returns a scalar by treating the input as a vector.\n\nAssume the input has size *k* on axis 1, then both ``gamma`` and ``beta``\nhave shape *(k,)*. If ``output_mean_var`` is set to be true, then outputs both ``data_mean`` and\nthe inverse of ``data_var``, which are needed for the backward pass. Note that gradient of these\ntwo outputs are blocked.\n\nBesides the inputs and the outputs, this operator accepts two auxiliary\nstates, ``moving_mean`` and ``moving_var``, which are *k*-length\nvectors. They are global statistics for the whole dataset, which are updated\nby::\n\n moving_mean = moving_mean * momentum + data_mean * (1 - momentum)\n moving_var = moving_var * momentum + data_var * (1 - momentum)\n\nIf ``use_global_stats`` is set to be true, then ``moving_mean`` and\n``moving_var`` are used instead of ``data_mean`` and ``data_var`` to compute\nthe output. It is often used during inference.\n\nThe parameter ``axis`` specifies which axis of the input shape denotes\nthe 'channel' (separately normalized groups). The default is 1. Specifying -1 sets the channel\naxis to be the last item in the input shape.\n\nBoth ``gamma`` and ``beta`` are learnable parameters. But if ``fix_gamma`` is true,\nthen set ``gamma`` to 1 and its gradient to 0.\n\n.. Note::\n When ``fix_gamma`` is set to True, no sparse support is provided. If ``fix_gamma is`` set to False,\n the sparse tensors will fallback.\n\n\n\nDefined in src/operator/nn/batch_norm.cc:L574"
[{:as opts,
[{:keys
[data
gamma
beta
moving-mean
moving-var
eps
momentum
fix-gamma
use-global-stats
output-mean-var
axis
cudnn-off
name
attr],
:or
{output-mean-var nil,
axis nil,
Expand All @@ -55,21 +69,7 @@
moving-var nil,
momentum nil,
gamma nil},
:keys
[data
gamma
beta
moving-mean
moving-var
eps
momentum
fix-gamma
use-global-stats
output-mean-var
axis
cudnn-off
name
attr]}]
:as opts}]
(util/coerce-return
(SymbolAPI/BatchNorm
(util/->option data)
Expand Down
26 changes: 13 additions & 13 deletions contrib/clojure-package/test/test-ndarray-api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
activation
"Applies an activation function element-wise to the input.\n\nThe following activation functions are supported:\n\n- `relu`: Rectified Linear Unit, :math:`y = max(x, 0)`\n- `sigmoid`: :math:`y = \\frac{1}{1 + exp(-x)}`\n- `tanh`: Hyperbolic tangent, :math:`y = \\frac{exp(x) - exp(-x)}{exp(x) + exp(-x)}`\n- `softrelu`: Soft ReLU, or SoftPlus, :math:`y = log(1 + exp(x))`\n- `softsign`: :math:`y = \\frac{x}{1 + abs(x)}`\n\n\n\nDefined in src/operator/nn/activation.cc:L167"
([data act-type] (activation {:data data, :act-type act-type}))
([{:as opts, :or {out nil}, :keys [data act-type out]}]
([{:keys [data act-type out], :or {out nil}, :as opts}]
(util/coerce-return
(NDArrayAPI/Activation data act-type (util/->option out)))))

Expand All @@ -45,17 +45,7 @@
:beta beta,
:moving-mean moving-mean,
:moving-var moving-var}))
([{:as opts,
:or
{eps nil,
momentum nil,
fix-gamma nil,
use-global-stats nil,
output-mean-var nil,
axis nil,
cudnn-off nil,
out nil},
:keys
([{:keys
[data
gamma
beta
Expand All @@ -68,7 +58,17 @@
output-mean-var
axis
cudnn-off
out]}]
out],
:or
{eps nil,
momentum nil,
fix-gamma nil,
use-global-stats nil,
output-mean-var nil,
axis nil,
cudnn-off nil,
out nil},
:as opts}]
(util/coerce-return
(NDArrayAPI/BatchNorm
data
Expand Down
36 changes: 18 additions & 18 deletions contrib/clojure-package/test/test-symbol-api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,30 @@
(defn
activation
"Applies an activation function element-wise to the input.\n\nThe following activation functions are supported:\n\n- `relu`: Rectified Linear Unit, :math:`y = max(x, 0)`\n- `sigmoid`: :math:`y = \\frac{1}{1 + exp(-x)}`\n- `tanh`: Hyperbolic tangent, :math:`y = \\frac{exp(x) - exp(-x)}{exp(x) + exp(-x)}`\n- `softrelu`: Soft ReLU, or SoftPlus, :math:`y = log(1 + exp(x))`\n- `softsign`: :math:`y = \\frac{x}{1 + abs(x)}`\n\n\n\nDefined in src/operator/nn/activation.cc:L167"
[{:as opts,
[{:keys [data act-type name attr],
:or {data nil, name nil, attr nil},
:keys [data act-type name attr]}]
:as opts}]
(util/coerce-return
(SymbolAPI/Activation (util/->option data) act-type name attr)))

(defn
batch-norm
"Batch normalization.\n\nNormalizes a data batch by mean and variance, and applies a scale ``gamma`` as\nwell as offset ``beta``.\n\nAssume the input has more than one dimension and we normalize along axis 1.\nWe first compute the mean and variance along this axis:\n\n.. math::\n\n data\\_mean[i] = mean(data[:,i,:,...]) \\\\\n data\\_var[i] = var(data[:,i,:,...])\n\nThen compute the normalized output, which has the same shape as input, as following:\n\n.. math::\n\n out[:,i,:,...] = \\frac{data[:,i,:,...] - data\\_mean[i]}{\\sqrt{data\\_var[i]+\\epsilon}} * gamma[i] + beta[i]\n\nBoth *mean* and *var* returns a scalar by treating the input as a vector.\n\nAssume the input has size *k* on axis 1, then both ``gamma`` and ``beta``\nhave shape *(k,)*. If ``output_mean_var`` is set to be true, then outputs both ``data_mean`` and\nthe inverse of ``data_var``, which are needed for the backward pass. Note that gradient of these\ntwo outputs are blocked.\n\nBesides the inputs and the outputs, this operator accepts two auxiliary\nstates, ``moving_mean`` and ``moving_var``, which are *k*-length\nvectors. They are global statistics for the whole dataset, which are updated\nby::\n\n moving_mean = moving_mean * momentum + data_mean * (1 - momentum)\n moving_var = moving_var * momentum + data_var * (1 - momentum)\n\nIf ``use_global_stats`` is set to be true, then ``moving_mean`` and\n``moving_var`` are used instead of ``data_mean`` and ``data_var`` to compute\nthe output. It is often used during inference.\n\nThe parameter ``axis`` specifies which axis of the input shape denotes\nthe 'channel' (separately normalized groups). The default is 1. Specifying -1 sets the channel\naxis to be the last item in the input shape.\n\nBoth ``gamma`` and ``beta`` are learnable parameters. But if ``fix_gamma`` is true,\nthen set ``gamma`` to 1 and its gradient to 0.\n\n.. Note::\n When ``fix_gamma`` is set to True, no sparse support is provided. If ``fix_gamma is`` set to False,\n the sparse tensors will fallback.\n\n\n\nDefined in src/operator/nn/batch_norm.cc:L574"
[{:as opts,
[{:keys
[data
gamma
beta
moving-mean
moving-var
eps
momentum
fix-gamma
use-global-stats
output-mean-var
axis
cudnn-off
name
attr],
:or
{output-mean-var nil,
axis nil,
Expand All @@ -55,21 +69,7 @@
moving-var nil,
momentum nil,
gamma nil},
:keys
[data
gamma
beta
moving-mean
moving-var
eps
momentum
fix-gamma
use-global-stats
output-mean-var
axis
cudnn-off
name
attr]}]
:as opts}]
(util/coerce-return
(SymbolAPI/BatchNorm
(util/->option data)
Expand Down

0 comments on commit ffb73f8

Please sign in to comment.