-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix load of models that depend on non thread-safe dependencies
A problem was detected when loading TensorFlow models in different threads inside the same JVM. That happened after load a TensorFlow model and then try to import a new TensorFlow model. This was caused by a dependency of TensorFlow (protobuf) that was being reloaded but it already existed in the JVM (through the 1st thread). The workaround was to share the problematic module (Tensorflow) across the sub-interpreters of Python. This is a workaround for the issues with CPython extensions.
- Loading branch information
Paulo Pereira
committed
Dec 13, 2018
1 parent
7f2d92f
commit a34f327
Showing
9 changed files
with
263 additions
and
5 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
103 changes: 103 additions & 0 deletions
103
openml-python-common/src/main/java/com/feedzai/openml/python/xml/parser/XMLParser.java
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,103 @@ | ||
/* | ||
* Copyright (c) 2018 Feedzai | ||
* | ||
* Licensed 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 com.feedzai.openml.python.xml.parser; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.NodeList; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import java.io.File; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Class responsible for parsing a XML file in order to retrieve the modules of Python to be shared across | ||
* sub-interpreters. | ||
* | ||
* @author Paulo Pereira (paulo.pereira@feedzai.com) | ||
* @since 0.1.5 | ||
*/ | ||
public class XMLParser { | ||
|
||
/** | ||
* Logger. | ||
*/ | ||
private static final Logger logger = LoggerFactory.getLogger(XMLParser.class); | ||
|
||
/** | ||
* A XML file with the modules of Python to be shared across sub-interpreters. | ||
*/ | ||
private static String XML_FILE; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public XMLParser() { | ||
XML_FILE = "python-packages.xml"; | ||
} | ||
|
||
/** | ||
* Gets the {@link File} reference of {@link #XML_FILE} that exists in the current classpath. | ||
* | ||
* @return The {@link File} reference of {@link #XML_FILE}. | ||
*/ | ||
private File getXMLFile() { | ||
final ClassLoader classLoader = getClass().getClassLoader(); | ||
return new File(classLoader.getResource(XML_FILE).getFile()); | ||
} | ||
|
||
/** | ||
* Parses the {@link #XML_FILE} to retrieve the modules of Python to be shared across sub-interpreters. | ||
* | ||
* @return A {@link Set} with the modules of Python to be shared across sub-interpreters. | ||
* @throws Exception If there is an error while parsing {@link #XML_FILE}. | ||
*/ | ||
private Set<String> parseXMLFile() throws Exception { | ||
final HashSet<String> sharedModules = new HashSet<>(); | ||
final File xmlFile = getXMLFile(); | ||
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | ||
final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | ||
final Document doc = dBuilder.parse(xmlFile); | ||
doc.getDocumentElement().normalize(); | ||
|
||
final NodeList nList = doc.getElementsByTagName("package"); | ||
|
||
for (int i = 0; i < nList.getLength(); i++) { | ||
sharedModules.add(nList.item(i).getFirstChild().getNodeValue()); | ||
} | ||
return sharedModules; | ||
} | ||
|
||
/** | ||
* Retrieves a {@link Set} with the modules of Python to be shared across sub-interpreters. | ||
* | ||
* @return The modules of Python to be shared across sub-interpreters. | ||
*/ | ||
public Set<String> getSharedModules() { | ||
Set<String> sharedModules = ImmutableSet.of(); | ||
try { | ||
sharedModules = parseXMLFile(); | ||
} catch (final Exception e) { | ||
logger.warn("Problem while getting the XML file with the shared modules of Python.", e); | ||
} | ||
return sharedModules; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
openml-python-common/src/main/java/com/feedzai/openml/python/xml/parser/package-info.java
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,23 @@ | ||
/* | ||
* Copyright (c) 2018 Feedzai | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
/** | ||
* This package contains logic for parsing XML files to retrieve the modules of Python to be shared across | ||
* sub-interpreters. | ||
* | ||
* @since 0.1.5 | ||
*/ | ||
package com.feedzai.openml.python.xml.parser; |
66 changes: 66 additions & 0 deletions
66
openml-python-common/src/test/java/com/feedzai/openml/python/xml/parser/XMLParserTest.java
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,66 @@ | ||
/* | ||
* Copyright (c) 2018 Feedzai | ||
* | ||
* Licensed 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 com.feedzai.openml.python.xml.parser; | ||
|
||
import mockit.Mock; | ||
import mockit.MockUp; | ||
import org.assertj.core.util.Files; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests the retrieving of the modules to be shared across sub-interpreters from a XML file. | ||
* | ||
* @author Paulo Pereira (paulo.pereira@feedzai.com) | ||
* @since 0.1.5 | ||
*/ | ||
public class XMLParserTest { | ||
|
||
/** | ||
* Tests the retrieving of the shared modules from a valid XML file. | ||
*/ | ||
@Test | ||
public void validXMLFileTest() { | ||
final Set<String> sharedPythonPackages = new XMLParser().getSharedModules(); | ||
assertThat(sharedPythonPackages) | ||
.as("Set of shared modules.") | ||
.hasSize(2) | ||
.contains("my_package_1", "my_package_2"); | ||
} | ||
|
||
/** | ||
* Tests the retrieving of the shared modules from an invalid XML file. | ||
*/ | ||
@Test | ||
public void invalidXMLFileTest() { | ||
final XMLParser xmlParser = new MockUp<XMLParser>() { | ||
@Mock | ||
private File getXMLFile() { | ||
final File file = Files.newTemporaryFile(); | ||
file.deleteOnExit(); | ||
return file; | ||
} | ||
}.getMockInstance(); | ||
assertThat(xmlParser.getSharedModules()) | ||
.as("Set of shared modules.") | ||
.hasSize(0); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
openml-python-common/src/test/java/com/feedzai/openml/python/xml/parser/package-info.java
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,22 @@ | ||
/* | ||
* Copyright (c) 2018 Feedzai | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
/** | ||
* This package contains the unit-tests for {@link com.feedzai.openml.python.xml.parser}. | ||
* | ||
* @since 0.1.5 | ||
*/ | ||
package com.feedzai.openml.python.xml.parser; |
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,5 @@ | ||
<?xml version="1.0"?> | ||
<python> | ||
<package>my_package_1</package> | ||
<package>my_package_2</package> | ||
</python> |
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