-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor.Importers { | ||
|
||
using UnityEditor.AssetImporters; | ||
using API.Types; | ||
|
||
[ScriptedImporter(1, @"mlmodel")] | ||
internal sealed class CoreMLImporter : GraphImporter { | ||
|
||
protected override PredictorSession CreateSession () => new PredictorSession { | ||
format = GraphFormat.CoreML | ||
}; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor.Importers { | ||
|
||
using System.IO; | ||
using UnityEngine; | ||
using UnityEditor.AssetImporters; | ||
using API.Types; | ||
|
||
public abstract class GraphImporter : ScriptedImporter { | ||
|
||
public sealed override void OnImportAsset (AssetImportContext ctx) { | ||
var modelData = ScriptableObject.CreateInstance<MLModelData>(); | ||
modelData.session = CreateSession(); | ||
modelData.graph = File.ReadAllBytes(ctx.assetPath); | ||
ctx.AddObjectToAsset("MLModelData", modelData); | ||
ctx.SetMainObject(modelData); | ||
} | ||
|
||
protected abstract PredictorSession CreateSession (); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor.Importers { | ||
|
||
using UnityEditor.AssetImporters; | ||
using API.Types; | ||
|
||
[ScriptedImporter(1, @"onnx")] | ||
internal sealed class ONNXImporter : GraphImporter { | ||
|
||
protected override PredictorSession CreateSession () => new PredictorSession { | ||
format = GraphFormat.ONNX | ||
}; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor.Importers { | ||
|
||
using UnityEditor.AssetImporters; | ||
using API.Types; | ||
|
||
[ScriptedImporter(1, @"tflite")] | ||
internal sealed class TFLiteImporter : GraphImporter { | ||
|
||
protected override PredictorSession CreateSession () => new PredictorSession { | ||
format = GraphFormat.TFLite | ||
}; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "NatML.ML.Editor", | ||
"references": [ | ||
"NatML.Hub", | ||
"NatML.Hub.Editor", | ||
"NatML.ML" | ||
], | ||
"includePlatforms": [ | ||
"Editor" | ||
], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor { | ||
|
||
using System.IO; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using Internal; | ||
|
||
internal static class NatMLMenu { | ||
|
||
private const int BasePriority = -50; | ||
|
||
[MenuItem(@"NatML/NatML " + NatML.Version, false, BasePriority)] | ||
private static void Version () { } | ||
|
||
[MenuItem(@"NatML/NatML " + NatML.Version, true, BasePriority)] | ||
private static bool EnableVersion () => false; | ||
|
||
[MenuItem(@"NatML/Explore Predictors", false, BasePriority + 1)] | ||
private static void OpenHub () => Help.BrowseURL(@"https://hub.natml.ai"); | ||
|
||
[MenuItem(@"NatML/View NatML Docs", false, BasePriority + 2)] | ||
private static void OpenDocs () => Help.BrowseURL(@"https://docs.natml.ai/unity"); | ||
|
||
[MenuItem(@"NatML/Open a NatML Issue", false, BasePriority + 3)] | ||
private static void OpenIssue () => Help.BrowseURL(@"https://github.com/natmlx/NatML"); | ||
|
||
[MenuItem(@"NatML/Clear Predictor Cache", false, BasePriority + 4)] | ||
private static void ClearCache () { | ||
MLEdgeModel.ClearCache(); | ||
Debug.Log("NatML: Cleared predictor cache"); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor { | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEditor; | ||
using UnityEditor.Build.Reporting; | ||
using UnityEngine; | ||
using API; | ||
using API.Types; | ||
using Hub.Editor; | ||
using Hub.Internal; | ||
using Internal; | ||
|
||
internal sealed class NatMLSettingsEmbed : BuildEmbedHelper<NatMLSettings> { | ||
|
||
protected override BuildTarget[] SupportedTargets => new [] { | ||
BuildTarget.Android, | ||
BuildTarget.iOS, | ||
BuildTarget.StandaloneOSX, | ||
BuildTarget.StandaloneWindows, | ||
BuildTarget.StandaloneWindows64, | ||
BuildTarget.WebGL, | ||
}; | ||
private const string CachePath = @"Assets/NMLBuildCache"; | ||
|
||
protected override NatMLSettings[] CreateEmbeds (BuildReport report) { | ||
// Check platform // We don't support embeds on Linux editor (#54) | ||
if (Application.platform == RuntimePlatform.LinuxEditor) | ||
return new NatMLSettings[0]; | ||
// Create cache path | ||
Directory.CreateDirectory(CachePath); | ||
// Create settings | ||
var settings = CreateSettings(); | ||
// Get embeds | ||
var embeds = AppDomain.CurrentDomain.GetAssemblies() | ||
.SelectMany(assembly => assembly | ||
.GetTypes() | ||
.SelectMany(type => Attribute.GetCustomAttributes(type, typeof(MLEdgeModel.EmbedAttribute))) | ||
) | ||
.Cast<MLEdgeModel.EmbedAttribute>() | ||
.ToArray(); | ||
var defaultAccessKey = HubSettings.Instance.AccessKey; | ||
var format = GetFormat(report.summary.platform); | ||
var secret = MLEdgeModel.CreateSecret().Result; // this completes immediately everywhere except web | ||
settings.embeds = Task.WhenAll(embeds.Select(e => Task.Run(async () => { | ||
var accessKey = !string.IsNullOrEmpty(e.accessKey) ? e.accessKey : defaultAccessKey; | ||
var client = new NatMLClient(accessKey); | ||
var session = await client.PredictorSessions.Create(e.tag, format, secret); | ||
var graphStream = await client.Storage.Download(session.graph); | ||
var graph = graphStream.ToArray(); | ||
var embed = new NatMLSettings.Embed { fingerprint = session.fingerprint, data = graph }; | ||
return embed; | ||
}))).Result; | ||
// Write settings | ||
AssetDatabase.CreateAsset(settings, $"{CachePath}/NatML.asset"); | ||
return new [] { settings }; | ||
} | ||
|
||
protected override void ClearEmbeds (BuildReport report) { | ||
base.ClearEmbeds(report); | ||
AssetDatabase.DeleteAsset(CachePath); | ||
} | ||
|
||
internal static NatMLSettings CreateSettings () { | ||
var settings = ScriptableObject.CreateInstance<NatMLSettings>(); | ||
settings.accessKey = HubSettings.Instance.AccessKey; | ||
return settings; | ||
} | ||
|
||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] | ||
private static void OnLoad () => NatMLSettings.Instance = CreateSettings(); | ||
|
||
private static GraphFormat GetFormat (BuildTarget target) => target switch { | ||
BuildTarget.Android => GraphFormat.TFLite, | ||
BuildTarget.iOS => GraphFormat.CoreML, | ||
BuildTarget.StandaloneOSX => GraphFormat.CoreML, | ||
BuildTarget.StandaloneWindows => GraphFormat.ONNX, | ||
BuildTarget.StandaloneWindows64 => GraphFormat.ONNX, | ||
BuildTarget.WebGL => GraphFormat.ONNX, | ||
_ => 0, | ||
}; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor { | ||
|
||
using UnityEditor; | ||
using UnityEditor.Build; | ||
using UnityEditor.Build.Reporting; | ||
|
||
internal sealed class WebGLBuildHelper : IPreprocessBuildWithReport { | ||
|
||
public int callbackOrder => 0; | ||
private readonly string[] EM_ARGS = new [] { | ||
@"--bind", | ||
}; | ||
|
||
void IPreprocessBuildWithReport.OnPreprocessBuild (BuildReport report) { | ||
if (report.summary.platform != BuildTarget.WebGL) | ||
return; | ||
foreach (var arg in EM_ARGS) { | ||
var standaloneArg = $" {arg} "; | ||
if (!PlayerSettings.WebGL.emscriptenArgs.Contains(standaloneArg)) | ||
PlayerSettings.WebGL.emscriptenArgs += standaloneArg; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.