This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Numpy] Java/Scala modification (#14625)
* modify jni to support 0 dim/shape * fix transpose axes default value
- Loading branch information
Showing
10 changed files
with
452 additions
and
142 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
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
58 changes: 58 additions & 0 deletions
58
scala-package/core/src/main/scala/org/apache/mxnet/NumpyScope.scala
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,58 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.mxnet | ||
|
||
import org.apache.mxnet.Base._ | ||
|
||
object NumpyScope { | ||
def setNumpyCompatible(isNpComp: Boolean): Boolean = { | ||
val prev = new RefInt() | ||
checkCall(_LIB.mxSetIsNumpyCompatible(if (isNpComp) 1 else 0, prev)) | ||
if (prev.value != 0) true else false | ||
} | ||
|
||
def isNumpyCompatible: Boolean = { | ||
val curr = new RefInt | ||
checkCall(_LIB.mxIsNumpyCompatible(curr)) | ||
if (curr.value != 0) true else false | ||
} | ||
|
||
def enableNumpyCompatible: NumpyScope = { | ||
new NumpyScope(true) | ||
} | ||
|
||
|
||
def disableNumpyCompatible: NumpyScope = { | ||
new NumpyScope(false) | ||
} | ||
} | ||
|
||
class NumpyScope(var isCompatible: Boolean) { | ||
private var prev: Boolean = false | ||
|
||
def withScope[T](body: => T): T = { | ||
prev = NumpyScope.setNumpyCompatible(isCompatible) | ||
try { | ||
body | ||
} finally { | ||
if (prev != isCompatible) { | ||
NumpyScope.setNumpyCompatible(prev) | ||
} | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
scala-package/core/src/test/scala/org/apache/mxnet/NumpyScopeSuite.scala
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,34 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.mxnet | ||
|
||
import org.scalatest.{BeforeAndAfterAll, FunSuite} | ||
|
||
class NumpyScopeSuite extends FunSuite with BeforeAndAfterAll { | ||
test("compatible") { | ||
NumpyScope.enableNumpyCompatible.withScope { | ||
assert(NumpyScope.isNumpyCompatible === true) | ||
} | ||
} | ||
|
||
test("incompatible") { | ||
NumpyScope.disableNumpyCompatible.withScope { | ||
assert(NumpyScope.isNumpyCompatible === false) | ||
} | ||
} | ||
} |
Oops, something went wrong.