-
Notifications
You must be signed in to change notification settings - Fork 1
Add derivatives for RealFunctions #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4662bca
Add derivatives for RealFunctions
JaapWijnen 448b9ce
formatting
JaapWijnen 8fd0b5a
add some tests and fix incorrect derivative
JaapWijnen bb000dc
process feedback
JaapWijnen 8caa407
use isApproximatelyEqual to compare
JaapWijnen d105d4f
add feedback
JaapWijnen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,36 @@ | ||
| import Foundation | ||
| import PackagePlugin | ||
|
|
||
| @main | ||
| struct CodeGeneratorPlugin: BuildToolPlugin { | ||
| func createBuildCommands(context: PackagePlugin.PluginContext, target _: PackagePlugin.Target) async throws -> [PackagePlugin.Command] { | ||
| let output = context.pluginWorkDirectoryURL | ||
|
|
||
| let floatingPointTypes: [String] = ["Float", "Double"] | ||
| let simdWidths = [2, 4, 8, 16, 32, 64] | ||
|
|
||
| let outputFiles = floatingPointTypes.flatMap { floatingPointType in | ||
| simdWidths.flatMap { simdWidth in | ||
| [ | ||
| output.appending(component: "SIMD\(simdWidth)+\(floatingPointType)+RealFunctions.swift"), | ||
| output.appending(component: "SIMD\(simdWidth)+\(floatingPointType)+RealFunctions+Derivatives.swift"), | ||
| ] | ||
| } + [ | ||
| output.appending(component: "\(floatingPointType)+RealFunctions+Derivatives.swift"), | ||
| ] | ||
| } + [ | ||
| output.appending(component: "SIMD+RealFunctions.swift"), | ||
| ] | ||
|
|
||
| return [ | ||
| .buildCommand( | ||
| displayName: "Generate Code", | ||
| executable: try context.tool(named: "CodeGeneratorExecutable").url, | ||
| arguments: [output.relativePath], | ||
| environment: [:], | ||
| inputFiles: [], | ||
| outputFiles: outputFiles | ||
| ), | ||
| ] | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,97 @@ | ||
| import Foundation | ||
|
|
||
| @main | ||
| struct CodeGenerator { | ||
| static func main() throws { | ||
| guard CommandLine.arguments.count == 2 else { | ||
| throw CodeGeneratorError.invalidArguments | ||
| } | ||
| // arguments[0] is the path to this command line tool | ||
| let output = URL(filePath: CommandLine.arguments[1]) | ||
|
|
||
| // generate default implementations of RealFunctions for SIMD protocol | ||
| let realFunctionSIMDFileURL = output.appending(component: "SIMD+RealFunctions.swift") | ||
| let realFunctionsSIMDExtension = RealFunctionsGenerator.realFunctionsExtension( | ||
| objectType: "SIMD", | ||
| type: "Self", | ||
| whereClause: true, | ||
| simdAccelerated: false | ||
| ) | ||
| try realFunctionsSIMDExtension.write(to: realFunctionSIMDFileURL, atomically: true, encoding: .utf8) | ||
|
|
||
| let floatingPointTypes: [String] = ["Float", "Double"] | ||
| let simdWidths: [Int] = [2, 4, 8, 16, 32, 64] | ||
|
|
||
| for floatingPointType in floatingPointTypes { | ||
| // Generator Derivatives for RealFunctions for floating point types | ||
| let realFunctionDerivativesFileURL = output.appending( | ||
| component: "\(floatingPointType)+RealFunctions+Derivatives.swift", | ||
| directoryHint: .notDirectory | ||
| ) | ||
| let realFunctionsDerivativesExtensionCode = RealFunctionsDerivativesGenerator.realFunctionsDerivativesExtension( | ||
| type: floatingPointType, | ||
| floatingPointType: floatingPointType | ||
| ) | ||
| try realFunctionsDerivativesExtensionCode.write(to: realFunctionDerivativesFileURL, atomically: true, encoding: .utf8) | ||
|
|
||
| for simdWidth in simdWidths { | ||
| let realFunctionFileURL = output.appending( | ||
| component: "SIMD\(simdWidth)+\(floatingPointType)+RealFunctions.swift", | ||
| directoryHint: .notDirectory | ||
| ) | ||
| let simdType = "SIMD\(simdWidth)<\(floatingPointType)>" | ||
|
|
||
| // no simd methods exist for simd size >= 16 and scalar > Float so we don't add acceleration to those. | ||
JaapWijnen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let simdAccelerated: Bool | ||
| if simdWidth > 16 || (simdWidth == 16 && floatingPointType == "Double") { | ||
| simdAccelerated = false | ||
| } | ||
| else { | ||
| simdAccelerated = true | ||
| } | ||
JaapWijnen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JaapWijnen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Generate RealFunctions implementations on concrete SIMD types to attach derivatives to | ||
| let realFunctionsExtensionCode = RealFunctionsGenerator.realFunctionsExtension( | ||
| objectType: simdType, | ||
| type: simdType, | ||
| whereClause: false, | ||
| simdAccelerated: simdAccelerated | ||
| ) | ||
| try realFunctionsExtensionCode.write(to: realFunctionFileURL, atomically: true, encoding: .utf8) | ||
|
|
||
| // Generate RealFunctions derivatives for concrete SIMD types | ||
| let realFunctionDerivativesFileURL = output | ||
| .appending(component: "SIMD\(simdWidth)+\(floatingPointType)+RealFunctions+Derivatives.swift") | ||
| let type = "SIMD\(simdWidth)<\(floatingPointType)>" | ||
| let realFunctionsDerivativesExtensionCode = RealFunctionsDerivativesGenerator.realFunctionsDerivativesExtension( | ||
| type: type, | ||
| floatingPointType: floatingPointType | ||
| ) | ||
| try realFunctionsDerivativesExtensionCode.write(to: realFunctionDerivativesFileURL, atomically: true, encoding: .utf8) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct RealFunction { | ||
| var name: String | ||
| var simdName: String? | ||
| var arguments: [Argument] | ||
|
|
||
| struct Argument { | ||
| var name: String | ||
| var label: String? | ||
| var type: String? | ||
| } | ||
|
|
||
| init(name: String, simdName: String? = nil, arguments: [Argument] = [.init(name: "x", label: "_")]) { | ||
| self.name = name | ||
| self.simdName = simdName | ||
| self.arguments = arguments | ||
| } | ||
| } | ||
|
|
||
| enum CodeGeneratorError: Error { | ||
| case invalidArguments | ||
| case invalidData | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: there are aliases Float16, Float32, Float64, so you could make this an array of bit widths rather than strings, which might clean up some other parts of the code?