How to use "@memberwise" attribute to initialize a struct/class

Hi,

I would like to know about the proper syntax on how to use the "@memberwise" while initializing a struct/class.

For example:

struct Model : @memberwise {
    let metadata1: Float
    let metadata2: Float

    init(_ m1:Float, _ m2:Float) {
        self.metadata1 = m1
        self.metadata2 = m2
    }
}

For the above syntax I am getting the following error while compiling the code.

 error: unknown attribute 'memberwise'
struct Model : @memberwise {

Any inputs will be helpful.

You cannot use it, because it does not exist AFAIK.

Hi @suyashsrijan, Thanks for your response.

I was reading about the DIfferentitable Programming feature. There In this document I see the examples being used with the below syntax at multiple places.

struct Model: @memberwise Differentiable {
    var weight: SIMD4<Double>
    var bias: Double
    let metadata1: Float
    let metadata2: Float
    let usesBias: Bool
}

How is it done in this case then?

That's only implemented in the TensorFlow branch of Swift. Not in the main branch

@jonprescott Thanks for your inputs. But I'm using the S4TF tool chain. It throws the same error. Also I thought Differentiable is already part of Swift5.3 and we could use that?

It’s not been implemented yet. The doc you’re reading is a “manifesto”, there are parts of it whose implementation is still in-progress

cc @dan-zheng @rxwei who can provide more information about it.

No, the TensorFlow branch hasn't been merged into the main app. I think TensorFlow has upgraded to 5.3, but, it's not the main branch 5.3

Thanks I will wait for @dan-zheng and @rxwei 's responses then.

Thanks for the clarification. I understand why it could fail in Swift5.3 but I'm not clear how could it fail for s4tf toolchain? I'm looking for the references for the same in the tensorflow brach. Will post my findings shortly.

Just to give some more inputs on the compile flags I'm currently using to execute the code.

swiftc -Xfrontend 
       -enable-experimental-forward-mode-differentiation   main.swift

I believe you can just import _Differentiation and it will turn it on, so no need to pass a flag.

Nope. I have tried it, here is the compete code.

import _Differentiation

struct Model : @memberwise Differentiable {
    let metadata1: Float
    let metadata2: Float

    init(_ m1:Float, _ m2:Float) {
        self.metadata1 = m1
        self.metadata2 = m2
    }
}

To build it I need to pass the explicit flag

After passing this flag this recognizes the import _Differentiation line.
I'm wondering if there is any additional flag which we need to pass to get it identify the "@memberwise" attribute?

Ah, maybe it doesn’t work with forward mode, only reverse mode.

As I said before, this attribute does not exist, so your code will not work regardless of whether you use a TensorFlow toolchain or a regular one.

1 Like

Thanks for the links.

I didn't find the "reverse mode" option. A quick grep on the options gave me the following options.

swiftc -h | grep "enable"
  -enable-astscope-lookup Enable ASTScope-based unqualified name lookup
  -enable-experimental-additive-arithmetic-derivation
  -enable-experimental-concise-pound-file
  -enable-experimental-cxx-interop
  -enable-experimental-forward-mode-differentiation
  -enable-library-evolution
  -enable-only-one-dependency-file
  -enable-request-based-incremental-dependencies
  -enable-source-range-dependencies
  -profile-use=<profdata> Supply a profdata file to enable profile-guided optimization

Any suggestions on which could be most appropriate option?

Hi @Santman, the @memberwise attribute as proposed in the Differentiable Programming Manifesto has not been implemented yet. Conformance synthesis for AdditiveArithmetic and Differentiable will be triggered automatically if you've imported the _Differentiation module and declared a conformance to one of these protocols.

import _Differentiation

struct Model : Differentiable {
    let metadata1: Float
    let metadata2: Float

    init(_ m1:Float, _ m2:Float) {
        self.metadata1 = m1
        self.metadata2 = m2
    }
}
1 Like

Thanks @rxwei for the clarification.

Terms of Service

Privacy Policy

Cookie Policy