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

Commit

Permalink
Julia: split symbolic-node.jl into several snippets (#14024)
Browse files Browse the repository at this point in the history
- `symbolic-node/type.jl`
- `symbolic-node/show.jl`
- `symbolic-node/arithmetic.jl`
- `symbolic-node/io.jl`
- `symbolic-node/array.jl`
- `symbolic-node/op.jl`
- `symbolic-node/autodiff.jl`

See also: #14001
  • Loading branch information
iblislin authored Mar 9, 2019
1 parent ce9e3cf commit ed83071
Show file tree
Hide file tree
Showing 8 changed files with 1,121 additions and 980 deletions.
987 changes: 7 additions & 980 deletions julia/src/symbolic-node.jl

Large diffs are not rendered by default.

127 changes: 127 additions & 0 deletions julia/src/symbolic-node/arithmetic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# 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.

import Base: +

"""
+(args...)
.+(args...)
Elementwise summation of `SymbolicNode`.
"""
function +(x::SymbolicNode, ys::SymbolicNodeOrReal...)
ret = x
for y ys
if y isa SymbolicNode
ret = _plus(ret, y)
else
ret = _plus_scalar(ret, scalar=MX_float(y))
end
end
ret
end

+(s::Real, x::SymbolicNode, ys::SymbolicNodeOrReal...) = +(x + s, ys...)

broadcasted(::typeof(+), x::SymbolicNode, ys::SymbolicNodeOrReal...) = +(x, ys...)
broadcasted(::typeof(+), s::Real, x::SymbolicNode, ys::SymbolicNodeOrReal...) = +(x + s, ys...)

import Base: -

"""
-(x, y)
.-(x, y)
Elementwise substraction of `SymbolicNode`.
Operating with `Real` is available.
"""
x::SymbolicNode - y::SymbolicNode = _minus(x, y)
x::SymbolicNode - s::Real = _minus_scalar(x, scalar=MX_float(s))
s::Real - x::SymbolicNode = _rminus_scalar(x, scalar=MX_float(s))

-(x::SymbolicNode) = 0 - x

broadcasted(::typeof(-), x::SymbolicNode, y::SymbolicNodeOrReal) = x - y
broadcasted(::typeof(-), s::Real, x::SymbolicNode) = s - x

import Base: *

"""
.*(x, y)
Elementwise multiplication of `SymbolicNode`.
"""
x::SymbolicNode * s::Real = _mul_scalar(x, scalar=MX_float(s))
s::Real * x::SymbolicNode = _mul_scalar(x, scalar=MX_float(s))

function broadcasted(::typeof(*), x::SymbolicNode, ys::SymbolicNodeOrReal...)
ret = x
for y in ys
if y isa SymbolicNode
ret = _mul(ret, y)
else
ret = _mul_scalar(ret, scalar=MX_float(y))
end
end
ret
end

broadcasted(::typeof(*), s::Real, x::SymbolicNode, ys::SymbolicNodeOrReal...) =
broadcasted(*, x * s, ys...)

import Base: /

"""
./(x, y)
* Elementwise dividing a `SymbolicNode` by a scalar or another `SymbolicNode`
of the same shape.
* Elementwise divide a scalar by an `SymbolicNode`.
* Matrix division (solving linear systems) is not implemented yet.
"""
x::SymbolicNode / s::Real = _DivScalar(x, scalar=MX_float(s))

broadcasted(::typeof(/), x::SymbolicNode, y::SymbolicNode) = _div(x, y)
broadcasted(::typeof(/), x::SymbolicNode, s::Real) = _div_scalar(x, scalar=MX_float(s))
broadcasted(::typeof(/), s::Real, x::SymbolicNode) = _rdiv_scalar(x, scalar=MX_float(s))


import Base: ^

"""
.^(x, y)
Elementwise power of `SymbolicNode` and `NDArray`.
Operating with `Real` is available.
"""
^

broadcasted(::typeof(^), x::SymbolicNode, y::SymbolicNode) = _power(x, y)
broadcasted(::typeof(^), x::SymbolicNode, s::Real) = _power_scalar(x, scalar = s)
broadcasted(::typeof(^), s::Real, x::SymbolicNode) = _rpower_scalar(x, scalar = s)
broadcasted(::typeof(Base.literal_pow), ::typeof(^), x::SymbolicNode, ::Val{s}) where {s} =
_power_scalar(x, scalar = s)

broadcasted(::typeof(^), ::Irrational{:ℯ}, x::SymbolicNode) = exp(x)
broadcasted(::typeof(^), x::SymbolicNode, s::Irrational) =
_power_scalar(x, scalar=MX_float(s))
broadcasted(::typeof(^), s::Irrational, x::SymbolicNode) =
_rpower_scalar(x, scalar=MX_float(s))


122 changes: 122 additions & 0 deletions julia/src/symbolic-node/array.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# 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.

# Base.Array related interface

import Base: reshape

"""
reshape(sym::SymbolicNode, dim; reverse=false, name)
reshape(sym::SymbolicNode, dim...; reverse=false, name)
Reshape SymbolicNode operator
Some dimensions of the shape can take special values from the set
{0, -1, -2, -3, -4}.
The significance of each is explained below:
- `0` copy this dimension from the input to the output shape.
Example:
- input shape = (2,3,4), shape = (4,0,2), output shape = (4,3,2)
- input shape = (2,3,4), shape = (2,0,0), output shape = (2,3,4)
- `-1` infers the dimension of the output shape by using the remainder of the
input dimensions keeping the size of the new array same as that of the input
array. At most one dimension of shape can be -1.
Example:
- input shape = (2,3,4), shape = (6,1,-1), output shape = (6,1,4)
- input shape = (2,3,4), shape = (3,-1,8), output shape = (3,1,8)
- input shape = (2,3,4), shape=(-1,), output shape = (24,)
- `-2` copy all/remainder of the input dimensions to the output shape.
Example:
- input shape = (2,3,4), shape = (-2,), output shape = (2,3,4)
- input shape = (2,3,4), shape = (2,-2), output shape = (2,3,4)
- input shape = (2,3,4), shape = (-2,1,1), output shape = (2,3,4,1,1)
- `-3` use the product of two consecutive dimensions of the input shape as the
output dimension.
Example:
- input shape = (2,3,4), shape = (-3,4), output shape = (6,4)
- input shape = (2,3,4,5), shape = (-3,-3), output shape = (6,20)
- input shape = (2,3,4), shape = (0,-3), output shape = (2,12)
- input shape = (2,3,4), shape = (-3,-2), output shape = (6,4)
- `-4` split one dimension of the input into two dimensions passed subsequent
to -4 in shape (can contain -1).
Example:
- input shape = (2,3,4), shape = (-4,1,2,-2), output shape = (1,2,3,4)
- input shape = (2,3,4), shape = (2,-4,-1,3,-2), output shape = (2,1,3,4)
If the argument `reverse` is set to `1`, then the special values are inferred
from right to left.
Example:
- with `reverse=false`, for input shape = (10,5,4), shape = (-1,0),
output shape would be (40,5)
- with `reverse=true`, output shape will be (50,4).
"""
reshape(sym::SymbolicNode, dim::NTuple{N, Integer}; kwargs...) where {N} =
_reshape(sym, dim; kwargs...)
reshape(sym::SymbolicNode, dim::Integer...; kwargs...) =
_reshape(sym, dim; kwargs...)

@inline function _reshape(sym::SymbolicNode, dim::NTuple{N,Integer};
reverse::Bool=false, name::String="") where N
op = _get_cached_libmx_op_handle("reshape")
node = _create_atomic_symbol(op.value, ["shape", "reverse"],
[dump_mx_param(dim), dump_mx_param(!reverse)])
name = get!(DEFAULT_NAME_MANAGER, name, "reshape")
_compose!(node, name=name, data=sym)
end

################################################################################
# Base.getindex
################################################################################

"""
getindex(self :: SymbolicNode, idx :: Union{Int, Base.Symbol, AbstractString})
Get a node representing the specified output of this node. The index could be
a symbol or string indicating the name of the output, or a 1-based integer
indicating the index, as in the list of [`list_outputs`](@ref).
"""
function Base.getindex(self :: SymbolicNode, idx :: Union{Base.Symbol, AbstractString})
idx = Symbol(idx)
i_idx = findall(idx .== list_outputs(self))
@assert(length(i_idx) > 0, "Cannot find output with name '$idx'")
@assert(length(i_idx) < 2, "Found duplicated output with name '$idx'")
Base.getindex(self, i_idx[1])
end
function Base.getindex(self :: SymbolicNode, idx :: Int)
ref_hdr = Ref{MX_handle}(0)
# note Julia is 1-based, while MXNet is 0-based
@mxcall(:MXSymbolGetOutput, (MX_handle, MX_uint, Ref{MX_handle}), self, idx-1, ref_hdr)
return SymbolicNode(MX_SymbolHandle(ref_hdr[]))
end

Loading

0 comments on commit ed83071

Please sign in to comment.