Commit 9e55a745 authored by Carlos Alfredo Yeverino Rodriguez's avatar Carlos Alfredo Yeverino Rodriguez
Browse files

Merge branch 'code_review_changes' into 'master'

Code review changes

See merge request !3
parents 0208cace 00580818
Pipeline #107085 passed with stages
in 4 minutes and 20 seconds
...@@ -51,18 +51,18 @@ public class CNNArch2Gluon implements CNNArchGenerator { ...@@ -51,18 +51,18 @@ public class CNNArch2Gluon implements CNNArchGenerator {
private boolean isSupportedLayer(ArchitectureElementSymbol element, LayerSupportChecker layerChecker){ private boolean isSupportedLayer(ArchitectureElementSymbol element, LayerSupportChecker layerChecker){
List<ArchitectureElementSymbol> constructLayerElemList; List<ArchitectureElementSymbol> constructLayerElemList;
if (!(element instanceof IOSymbol) && (element.getResolvedThis().get() instanceof CompositeElementSymbol)) if (!(element instanceof IOSymbol) && (element.getResolvedThis().get() instanceof CompositeElementSymbol)) {
{
constructLayerElemList = ((CompositeElementSymbol)element.getResolvedThis().get()).getElements(); constructLayerElemList = ((CompositeElementSymbol)element.getResolvedThis().get()).getElements();
for (ArchitectureElementSymbol constructedLayerElement : constructLayerElemList) { for (ArchitectureElementSymbol constructedLayerElement : constructLayerElemList) {
if (!isSupportedLayer(constructedLayerElement, layerChecker)) return false; if (!isSupportedLayer(constructedLayerElement, layerChecker)) {
return false;
}
} }
} }
if (!layerChecker.isSupported(element.toString())) { if (!layerChecker.isSupported(element.toString())) {
Log.error("Unsupported layer " + "'" + element.getName() + "'" + " for the backend MXNET."); Log.error("Unsupported layer " + "'" + element.getName() + "'" + " for the backend MXNET.");
return false; return false;
} } else {
else {
return true; return true;
} }
} }
...@@ -70,11 +70,18 @@ public class CNNArch2Gluon implements CNNArchGenerator { ...@@ -70,11 +70,18 @@ public class CNNArch2Gluon implements CNNArchGenerator {
private boolean supportCheck(ArchitectureSymbol architecture){ private boolean supportCheck(ArchitectureSymbol architecture){
LayerSupportChecker layerChecker = new LayerSupportChecker(); LayerSupportChecker layerChecker = new LayerSupportChecker();
for (ArchitectureElementSymbol element : ((CompositeElementSymbol)architecture.getBody()).getElements()){ for (ArchitectureElementSymbol element : ((CompositeElementSymbol)architecture.getBody()).getElements()){
if(!isSupportedLayer(element, layerChecker)) return false; if(!isSupportedLayer(element, layerChecker)) {
return false;
}
} }
return true; return true;
} }
private static void quitGeneration(){
Log.error("Code generation is aborted");
System.exit(1);
}
public CNNArch2Gluon() { public CNNArch2Gluon() {
setGenerationTargetPath("./target/generated-sources-cnnarch/"); setGenerationTargetPath("./target/generated-sources-cnnarch/");
} }
...@@ -105,19 +112,17 @@ public class CNNArch2Gluon implements CNNArchGenerator { ...@@ -105,19 +112,17 @@ public class CNNArch2Gluon implements CNNArchGenerator {
Optional<CNNArchCompilationUnitSymbol> compilationUnit = scope.resolve(rootModelName, CNNArchCompilationUnitSymbol.KIND); Optional<CNNArchCompilationUnitSymbol> compilationUnit = scope.resolve(rootModelName, CNNArchCompilationUnitSymbol.KIND);
if (!compilationUnit.isPresent()){ if (!compilationUnit.isPresent()){
Log.error("could not resolve architecture " + rootModelName); Log.error("could not resolve architecture " + rootModelName);
System.exit(1); quitGeneration();
} }
CNNArchCocos.checkAll(compilationUnit.get()); CNNArchCocos.checkAll(compilationUnit.get());
if (!supportCheck(compilationUnit.get().getArchitecture())){ if (!supportCheck(compilationUnit.get().getArchitecture())){
Log.error("Code generation aborted."); quitGeneration();
System.exit(1);
} }
try{ try{
generateFiles(compilationUnit.get().getArchitecture()); generateFiles(compilationUnit.get().getArchitecture());
} } catch (IOException e){
catch (IOException e){
Log.error(e.toString()); Log.error(e.toString());
} }
} }
...@@ -178,7 +183,7 @@ public class CNNArch2Gluon implements CNNArchGenerator { ...@@ -178,7 +183,7 @@ public class CNNArch2Gluon implements CNNArchGenerator {
try { try {
generateFromFilecontentsMap(fileContentMap); generateFromFilecontentsMap(fileContentMap);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.error("CMake file could not be generated" + e.getMessage());
} }
} }
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
* ******************************************************************************* * *******************************************************************************
*/ */
package de.monticore.lang.monticar.cnnarch.gluongenerator; package de.monticore.lang.monticar.cnnarch.gluongenerator;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
...@@ -73,13 +74,18 @@ public class CNNArch2GluonCli { ...@@ -73,13 +74,18 @@ public class CNNArch2GluonCli {
try { try {
cliArgs = parser.parse(options, args); cliArgs = parser.parse(options, args);
} catch (ParseException e) { } catch (ParseException e) {
System.err.println("argument parsing exception: " + e.getMessage()); Log.error("argument parsing exception: " + e.getMessage());
System.exit(1); quitGeneration();
return null; return null;
} }
return cliArgs; return cliArgs;
} }
private static void quitGeneration(){
Log.error("Code generation is aborted");
System.exit(1);
}
private static void runGenerator(CommandLine cliArgs) { private static void runGenerator(CommandLine cliArgs) {
Path modelsDirPath = Paths.get(cliArgs.getOptionValue(OPTION_MODELS_PATH.getOpt())); Path modelsDirPath = Paths.get(cliArgs.getOptionValue(OPTION_MODELS_PATH.getOpt()));
String rootModelName = cliArgs.getOptionValue(OPTION_ROOT_MODEL.getOpt()); String rootModelName = cliArgs.getOptionValue(OPTION_ROOT_MODEL.getOpt());
......
...@@ -92,8 +92,7 @@ public class CNNArchTemplateController { ...@@ -92,8 +92,7 @@ public class CNNArchTemplateController {
if (isSoftmaxOutput(layer) || isLogisticRegressionOutput(layer)){ if (isSoftmaxOutput(layer) || isLogisticRegressionOutput(layer)){
inputNames = getLayerInputs(layer.getInputElement().get()); inputNames = getLayerInputs(layer.getInputElement().get());
} } else {
else {
for (ArchitectureElementSymbol input : layer.getPrevious()) { for (ArchitectureElementSymbol input : layer.getPrevious()) {
if (input.getOutputTypes().size() == 1) { if (input.getOutputTypes().size() == 1) {
inputNames.add(getName(input)); inputNames.add(getName(input));
...@@ -148,12 +147,10 @@ public class CNNArchTemplateController { ...@@ -148,12 +147,10 @@ public class CNNArchTemplateController {
if (ioElement.isAtomic()){ if (ioElement.isAtomic()){
if (ioElement.isInput()){ if (ioElement.isInput()){
include(TEMPLATE_ELEMENTS_DIR_PATH, "Input", writer, netDefinitionMode); include(TEMPLATE_ELEMENTS_DIR_PATH, "Input", writer, netDefinitionMode);
} } else {
else {
include(TEMPLATE_ELEMENTS_DIR_PATH, "Output", writer, netDefinitionMode); include(TEMPLATE_ELEMENTS_DIR_PATH, "Output", writer, netDefinitionMode);
} }
} } else {
else {
include(ioElement.getResolvedThis().get(), writer, netDefinitionMode); include(ioElement.getResolvedThis().get(), writer, netDefinitionMode);
} }
...@@ -170,8 +167,7 @@ public class CNNArchTemplateController { ...@@ -170,8 +167,7 @@ public class CNNArchTemplateController {
String templateName = layer.getDeclaration().getName(); String templateName = layer.getDeclaration().getName();
include(TEMPLATE_ELEMENTS_DIR_PATH, templateName, writer, netDefinitionMode); include(TEMPLATE_ELEMENTS_DIR_PATH, templateName, writer, netDefinitionMode);
} }
} } else {
else {
include(layer.getResolvedThis().get(), writer, netDefinitionMode); include(layer.getResolvedThis().get(), writer, netDefinitionMode);
} }
...@@ -192,11 +188,9 @@ public class CNNArchTemplateController { ...@@ -192,11 +188,9 @@ public class CNNArchTemplateController {
public void include(ArchitectureElementSymbol architectureElement, Writer writer, NetDefinitionMode netDefinitionMode){ public void include(ArchitectureElementSymbol architectureElement, Writer writer, NetDefinitionMode netDefinitionMode){
if (architectureElement instanceof CompositeElementSymbol){ if (architectureElement instanceof CompositeElementSymbol){
include((CompositeElementSymbol) architectureElement, writer, netDefinitionMode); include((CompositeElementSymbol) architectureElement, writer, netDefinitionMode);
} } else if (architectureElement instanceof LayerSymbol){
else if (architectureElement instanceof LayerSymbol){
include((LayerSymbol) architectureElement, writer, netDefinitionMode); include((LayerSymbol) architectureElement, writer, netDefinitionMode);
} } else {
else {
include((IOSymbol) architectureElement, writer, netDefinitionMode); include((IOSymbol) architectureElement, writer, netDefinitionMode);
} }
} }
...@@ -213,15 +207,15 @@ public class CNNArchTemplateController { ...@@ -213,15 +207,15 @@ public class CNNArchTemplateController {
} }
public Map.Entry<String,String> process(String templateNameWithoutEnding, Target targetLanguage){ public Map.Entry<String,String> process(String templateNameWithoutEnding, Target targetLanguage){
StringWriter writer = new StringWriter(); StringWriter newWriter = new StringWriter();
this.mainTemplateNameWithoutEnding = templateNameWithoutEnding; this.mainTemplateNameWithoutEnding = templateNameWithoutEnding;
this.targetLanguage = targetLanguage; this.targetLanguage = targetLanguage;
this.writer = writer; this.writer = newWriter;
include("", templateNameWithoutEnding, writer); include("", templateNameWithoutEnding, newWriter);
String fileEnding = targetLanguage.toString(); String fileEnding = targetLanguage.toString();
String fileName = getFileNameWithoutEnding() + fileEnding; String fileName = getFileNameWithoutEnding() + fileEnding;
Map.Entry<String,String> fileContent = new AbstractMap.SimpleEntry<>(fileName, writer.toString()); Map.Entry<String,String> fileContent = new AbstractMap.SimpleEntry<>(fileName, newWriter.toString());
this.mainTemplateNameWithoutEnding = null; this.mainTemplateNameWithoutEnding = null;
this.targetLanguage = null; this.targetLanguage = null;
...@@ -265,12 +259,12 @@ public class CNNArchTemplateController { ...@@ -265,12 +259,12 @@ public class CNNArchTemplateController {
} }
private boolean isTOutput(Class inputPredefinedLayerClass, ArchitectureElementSymbol architectureElement){ private boolean isTOutput(Class inputPredefinedLayerClass, ArchitectureElementSymbol architectureElement){
if (architectureElement.isOutput()){ if (architectureElement.isOutput()
if (architectureElement.getInputElement().isPresent() && architectureElement.getInputElement().get() instanceof LayerSymbol){ && architectureElement.getInputElement().isPresent()
LayerSymbol inputLayer = (LayerSymbol) architectureElement.getInputElement().get(); && architectureElement.getInputElement().get() instanceof LayerSymbol){
if (inputPredefinedLayerClass.isInstance(inputLayer.getDeclaration())){ LayerSymbol inputLayer = (LayerSymbol) architectureElement.getInputElement().get();
return true; if (inputPredefinedLayerClass.isInstance(inputLayer.getDeclaration())){
} return true;
} }
} }
return false; return false;
......
...@@ -37,7 +37,9 @@ public class CNNTrain2Gluon implements CNNTrainGenerator { ...@@ -37,7 +37,9 @@ public class CNNTrain2Gluon implements CNNTrainGenerator {
it = configuration.getEntryMap().keySet().iterator(); it = configuration.getEntryMap().keySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
String key = it.next().toString(); String key = it.next().toString();
if (funcChecker.getUnsupportedElemList().contains(key)) it.remove(); if (funcChecker.getUnsupportedElemList().contains(key)) {
it.remove();
}
} }
} }
...@@ -52,12 +54,19 @@ public class CNNTrain2Gluon implements CNNTrainGenerator { ...@@ -52,12 +54,19 @@ public class CNNTrain2Gluon implements CNNTrainGenerator {
Iterator it = configuration.getOptimizer().getOptimizerParamMap().keySet().iterator(); Iterator it = configuration.getOptimizer().getOptimizerParamMap().keySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
String key = it.next().toString(); String key = it.next().toString();
if (funcChecker.getUnsupportedElemList().contains(key)) it.remove(); if (funcChecker.getUnsupportedElemList().contains(key)) {
it.remove();
}
} }
} }
} }
} }
private static void quitGeneration(){
Log.error("Code generation is aborted");
System.exit(1);
}
public CNNTrain2Gluon() { public CNNTrain2Gluon() {
setGenerationTargetPath("./target/generated-sources-cnnarch/"); setGenerationTargetPath("./target/generated-sources-cnnarch/");
} }
...@@ -89,7 +98,7 @@ public class CNNTrain2Gluon implements CNNTrainGenerator { ...@@ -89,7 +98,7 @@ public class CNNTrain2Gluon implements CNNTrainGenerator {
Optional<CNNTrainCompilationUnitSymbol> compilationUnit = scope.resolve(rootModelName, CNNTrainCompilationUnitSymbol.KIND); Optional<CNNTrainCompilationUnitSymbol> compilationUnit = scope.resolve(rootModelName, CNNTrainCompilationUnitSymbol.KIND);
if (!compilationUnit.isPresent()) { if (!compilationUnit.isPresent()) {
Log.error("could not resolve training configuration " + rootModelName); Log.error("could not resolve training configuration " + rootModelName);
System.exit(1); quitGeneration();
} }
setInstanceName(compilationUnit.get().getFullName()); setInstanceName(compilationUnit.get().getFullName());
CNNTrainCocos.checkAll(compilationUnit.get()); CNNTrainCocos.checkAll(compilationUnit.get());
...@@ -107,7 +116,7 @@ public class CNNTrain2Gluon implements CNNTrainGenerator { ...@@ -107,7 +116,7 @@ public class CNNTrain2Gluon implements CNNTrainGenerator {
genCPP.generateFile(new FileContent(fileContents.get(fileName), fileName)); genCPP.generateFile(new FileContent(fileContents.get(fileName), fileName));
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.error("CNNTrainer file could not be generated" + e.getMessage());
} }
} }
......
...@@ -89,8 +89,7 @@ public class ConfigurationData { ...@@ -89,8 +89,7 @@ public class ConfigurationData {
Class realClass = entry.getValue().getValue().getValue().getClass(); Class realClass = entry.getValue().getValue().getValue().getClass();
if (realClass == Boolean.class) { if (realClass == Boolean.class) {
valueAsString = (Boolean) entry.getValue().getValue().getValue() ? "True" : "False"; valueAsString = (Boolean) entry.getValue().getValue().getValue() ? "True" : "False";
} } else if (lrPolicyClasses.contains(realClass)) {
else if (lrPolicyClasses.contains(realClass)) {
valueAsString = "'" + valueAsString + "'"; valueAsString = "'" + valueAsString + "'";
} }
mapToStrings.put(paramName, valueAsString); mapToStrings.put(paramName, valueAsString);
......
...@@ -47,17 +47,14 @@ public class LayerNameCreator { ...@@ -47,17 +47,14 @@ public class LayerNameCreator {
protected int name(ArchitectureElementSymbol architectureElement, int stage, List<Integer> streamIndices){ protected int name(ArchitectureElementSymbol architectureElement, int stage, List<Integer> streamIndices){
if (architectureElement instanceof CompositeElementSymbol){ if (architectureElement instanceof CompositeElementSymbol){
return nameComposite((CompositeElementSymbol) architectureElement, stage, streamIndices); return nameComposite((CompositeElementSymbol) architectureElement, stage, streamIndices);
} } else{
else{
if (architectureElement.isAtomic()){ if (architectureElement.isAtomic()){
if (architectureElement.getMaxSerialLength().get() > 0){ if (architectureElement.getMaxSerialLength().get() > 0){
return add(architectureElement, stage, streamIndices); return add(architectureElement, stage, streamIndices);
} } else {
else {
return stage; return stage;
} }
} } else {
else {
ArchitectureElementSymbol resolvedElement = architectureElement.getResolvedThis().get(); ArchitectureElementSymbol resolvedElement = architectureElement.getResolvedThis().get();
return name(resolvedElement, stage, streamIndices); return name(resolvedElement, stage, streamIndices);
} }
...@@ -78,8 +75,7 @@ public class LayerNameCreator { ...@@ -78,8 +75,7 @@ public class LayerNameCreator {
streamIndices.remove(lastIndex); streamIndices.remove(lastIndex);
return Collections.max(endStages) + 1; return Collections.max(endStages) + 1;
} } else {
else {
int endStage = stage; int endStage = stage;
for (ArchitectureElementSymbol subElement : compositeElement.getElements()){ for (ArchitectureElementSymbol subElement : compositeElement.getElements()){
endStage = name(subElement, endStage, streamIndices); endStage = name(subElement, endStage, streamIndices);
...@@ -113,8 +109,7 @@ public class LayerNameCreator { ...@@ -113,8 +109,7 @@ public class LayerNameCreator {
name = name + "_" + arrayAccess + "_"; name = name + "_" + arrayAccess + "_";
} }
return name; return name;
} } else {
else {
return createBaseName(architectureElement) + stage + createStreamPostfix(streamIndices) + "_"; return createBaseName(architectureElement) + stage + createStreamPostfix(streamIndices) + "_";
} }
} }
...@@ -132,11 +127,9 @@ public class LayerNameCreator { ...@@ -132,11 +127,9 @@ public class LayerNameCreator {
} else { } else {
return layerDeclaration.getName().toLowerCase(); return layerDeclaration.getName().toLowerCase();
} }
} } else if (architectureElement instanceof CompositeElementSymbol){
else if (architectureElement instanceof CompositeElementSymbol){
return "group"; return "group";
} } else {
else {
return architectureElement.getName(); return architectureElement.getName();
} }
} }
......
...@@ -43,6 +43,11 @@ public class TemplateConfiguration { ...@@ -43,6 +43,11 @@ public class TemplateConfiguration {
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
} }
private static void quitGeneration(){
Log.error("Code generation is aborted");
System.exit(1);
}
public Configuration getConfiguration() { public Configuration getConfiguration() {
return configuration; return configuration;
} }
...@@ -58,14 +63,12 @@ public class TemplateConfiguration { ...@@ -58,14 +63,12 @@ public class TemplateConfiguration {
try{ try{
Template template = TemplateConfiguration.get().getTemplate(templatePath); Template template = TemplateConfiguration.get().getTemplate(templatePath);
template.process(ftlContext, writer); template.process(ftlContext, writer);
} } catch (IOException e) {
catch (IOException e) {
Log.error("Freemarker could not find template " + templatePath + " :\n" + e.getMessage()); Log.error("Freemarker could not find template " + templatePath + " :\n" + e.getMessage());
System.exit(1); quitGeneration();
} } catch (TemplateException e){
catch (TemplateException e){
Log.error("An exception occured in template " + templatePath + " :\n" + e.getMessage()); Log.error("An exception occured in template " + templatePath + " :\n" + e.getMessage());
System.exit(1); quitGeneration();
} }
} }
......
...@@ -25,12 +25,14 @@ public class TrainParamSupportChecker implements CNNTrainVisitor { ...@@ -25,12 +25,14 @@ public class TrainParamSupportChecker implements CNNTrainVisitor {
public TrainParamSupportChecker() { public TrainParamSupportChecker() {
} }
public String unsupportedOptFlag = "unsupported_optimizer"; public static final String unsupportedOptFlag = "unsupported_optimizer";
public List getUnsupportedElemList(){ public List getUnsupportedElemList(){
return this.unsupportedElemList; return this.unsupportedElemList;
} }
//Empty visit method denotes that the corresponding training parameter is supported.
//To set a training parameter as unsupported, add the corresponding node to the unsupportedElemList
public void visit(ASTNumEpochEntry node){} public void visit(ASTNumEpochEntry node){}
public void visit(ASTBatchSizeEntry node){} public void visit(ASTBatchSizeEntry node){}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment